Skip to content

SourceGen: VisualStateManager.VisualStateGroups causes 'Names must be unique' at startup #34716

@StephaneDelcroix

Description

@StephaneDelcroix

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

  1. 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>
  1. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-toolingXAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debuggings/triagedIssue has been reviewed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions