Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TextBox in AdornerLayer causes collection modified exception #14484

Conversation

BAndysc
Copy link
Contributor

@BAndysc BAndysc commented Feb 4, 2024

What does the pull request do?

This PR changes VisualLayerManager so that layers can be added during its Measure pass.

What is the current behavior?

Currently, adding a layer in VisualLayerManager during a Measure pass will cause Collection Modified exception to be thrown.

When a layer can be added during a Measure pass:

When a TextBox with non empty Text is added to an AdornerLayer and no text box has ever been focused yet, the next Measure pass will run ApplyTemplate on the textbox, this will access the VisualLayerManager.TextSelectorLayer property, the getter will create the layer and Collection Was Modified while Enumeration exception will be thrown, because all those methods will be run from within VisualLayerManager's MeasureOverride, which iterates through its layers.

What is the updated/expected behavior with this PR?

Layers can be added during measure pass, which means a textbox can be added to the adorner layer without a crash.

How was the solution implemented (if it's not obvious)?

Instead of a for each loop, let's use a traditional for loop. I think it is fine since layers are never removed - only added. So even if the layer is added during the measure pass, it will still be correctly processed in the very same loop.

Checklist

Breaking changes

n/a

Obsoletions / Deprecations

n/a

Fixed issues

Fixes #14483

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.1.999-cibuild0044426-beta. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6 maxkatz6 requested a review from emmauss February 5, 2024 00:40
@maxkatz6 maxkatz6 added this pull request to the merge queue Feb 8, 2024
Merged via the queue into AvaloniaUI:master with commit 08b422f Feb 8, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TextBox in AdornerLayer causes collection modified exception
4 participants