Description
When using VisualStateManager.VisualStateGroups in XAML with the source generator, the app throws InvalidOperationException: VisualStateGroup Names must be unique at first startup.
Expected Behavior
The page should load without errors — each VisualStateGroup name appears only once in the XAML.
Actual Behavior
System.InvalidOperationException: VisualStateGroup Names must be unique
at Microsoft.Maui.Controls.VisualStateManager.Validate(...)
Steps to Reproduce
- Create a page with a Button that has VisualStateManager.VisualStateGroups:
<Button Text="Press me">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualState Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="White" />
</VisualState.Setters>
</VisualState>
<VisualState Name="Pressed">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="LightGray" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
- Build and run — crashes at startup
Root Cause
The SG-generated InitializeComponent processes the VisualStateManager.VisualStateGroups collection-type attached property. During processing, the VisualStateGroupList validation callback fires and finds a duplicate "CommonStates" name — likely because the group is added to the collection during construction and then the list is assigned to the property, triggering re-validation against a pre-existing entry.
Affected Area
- XAML Source Generator (SourceGen)
SetPropertiesVisitor handling of collection-type attached properties with named items
Description
When using
VisualStateManager.VisualStateGroupsin XAML with the source generator, the app throwsInvalidOperationException: VisualStateGroup Names must be uniqueat first startup.Expected Behavior
The page should load without errors — each VisualStateGroup name appears only once in the XAML.
Actual Behavior
Steps to Reproduce
Root Cause
The SG-generated
InitializeComponentprocesses theVisualStateManager.VisualStateGroupscollection-type attached property. During processing, theVisualStateGroupListvalidation callback fires and finds a duplicate "CommonStates" name — likely because the group is added to the collection during construction and then the list is assigned to the property, triggering re-validation against a pre-existing entry.Affected Area
SetPropertiesVisitorhandling of collection-type attached properties with named items