Repository navigation
Fix stale StatesChanged subscription when replacing VisualStateGroupList item #37340
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -415,7 +415,17 @@ public void RemoveAt(int index) | |
| public VisualStateGroup this[int index] | ||
| { | ||
| get => _internalList[index]; | ||
| set => _internalList[index] = value; | ||
| set | ||
| { | ||
| if (value == null) | ||
| throw new ArgumentNullException(nameof(value)); | ||
|
|
||
| var oldGroup = _internalList[index]; | ||
| oldGroup.StatesChanged -= ValidateAndNotify; | ||
| _internalList[index] = value; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[major] Logic and Correctness — The replaced group's currently-applied setters are never un-applied. |
||
| value.StatesChanged += ValidateAndNotify; | ||
| ValidateAndNotify(_internalList); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[moderate] Logic and Correctness — |
||
| } | ||
| } | ||
|
|
||
| WeakReference<VisualElement> _visualElement; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,6 +153,54 @@ public void GroupWithDuplicateNameReplacesExisting() | |
| Assert.Same(secondGroup, vsgs[0]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReplacingGroupUpdatesVisualStateAndValidationSubscription() | ||
| { | ||
| var label = new Label(); | ||
| var originalMargin = new Thickness(1); | ||
| var replacementMargin = new Thickness(2); | ||
| var groups = new VisualStateGroupList | ||
|
Comment on lines
+156
to
+162
|
||
| { | ||
| new VisualStateGroup | ||
| { | ||
| States = | ||
| { | ||
| new VisualState | ||
| { | ||
| Name = NormalStateName, | ||
| Setters = | ||
| { | ||
| new Setter { Property = View.MarginProperty, Value = originalMargin } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| VisualStateManager.SetVisualStateGroups(label, groups); | ||
| Assert.Equal(originalMargin, label.Margin); | ||
|
|
||
| var replacementGroup = new VisualStateGroup | ||
| { | ||
| States = | ||
| { | ||
| new VisualState | ||
| { | ||
| Name = NormalStateName, | ||
| Setters = | ||
| { | ||
| new Setter { Property = View.MarginProperty, Value = replacementMargin } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| groups[0] = replacementGroup; | ||
|
|
||
| Assert.Equal(replacementMargin, label.Margin); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[moderate] Regression Prevention and Test Coverage — The fixture conflates two signals: the original and replacement states both set the same property ( |
||
| Assert.Throws<InvalidOperationException>(() => replacementGroup.States.Add(new VisualState { Name = NormalStateName })); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[major] Regression Prevention and Test Coverage — This test does not prove the stale-subscription half of the fix. |
||
| } | ||
|
|
||
| [Fact] | ||
| public void StateNamesInGroupMayNotBeNull() | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[moderate] Logic and Correctness — The
-=/+=pair is unconditional and does not account for the sameVisualStateGroupinstance appearing at more than one index. Two concrete cases: (1)groups[1] = groups[0]subscribesValidateAndNotifyto that instance a second time, so every laterStatesmutation on it runsValidate+OnStatesChanged(and thereforeChangeVisualState) twice; (2) ifoldGroupis also stored at another index,oldGroup.StatesChanged -= ValidateAndNotifyremoves the only handler, so the surviving copy's laterStatesmutations are no longer validated or propagated to the element. Guard withif (ReferenceEquals(oldGroup, value)) return;and only unsubscribe/subscribe when the instance is not otherwise present in_internalList.