Skip to content

Setting certain controls in VisualState crash the app #10264

@HO-COOH

Description

@HO-COOH

Describe the bug

I am setting ContentPresenter.Content to some controls in a VisualState. It crashes the app on certain larger / more complex controls, but it's okay for basic Button.

It's kind of a weird and corner-case usage I know, but if it's by design it should be documented somewhere.

Steps to reproduce the bug

  1. Use this xaml
  <StackPanel
      HorizontalAlignment="Center"
      VerticalAlignment="Center"
      Orientation="Vertical">
      <ComboBox Header="Select state:" SelectionChanged="ComboBox_SelectionChanged">
          <x:String>State1</x:String>
          <x:String>State2</x:String>
      </ComboBox>

      <ContentPresenter x:Name="Presenter" />

      <VisualStateManager.VisualStateGroups>
          <VisualStateGroup>
              <VisualState x:Name="State1">
                  <VisualState.Setters>
                      <Setter Target="Presenter.Content">
                          <Setter.Value>
                              <CalendarView />
                          </Setter.Value>
                      </Setter>
                  </VisualState.Setters>
              </VisualState>

              <VisualState x:Name="State2">
                  <VisualState.Setters>
                      <Setter Target="Presenter.Content">
                          <Setter.Value>
                              <Button
                                  x:Name="State2Button"
                                  Click="State2Button_Click"
                                  Content="State 2 Button" />
                          </Setter.Value>
                      </Setter>
                  </VisualState.Setters>
              </VisualState>
          </VisualStateGroup>
      </VisualStateManager.VisualStateGroups>
  </StackPanel>
  1. In code behind, you simply switch states according to the selected ComboBox item.
        void ComboBox_SelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e)
        {
            winrt::check_bool(winrt::Microsoft::UI::Xaml::VisualStateManager::GoToState(
                *this,
                sender.as<winrt::Microsoft::UI::Xaml::Controls::ComboBox>().SelectedItem().as<winrt::hstring>(),
                false
            ));
        }
  1. Build and run. Switch the state from the comboBox the first time, it works without issue. But switch back to the same state, it crashes.

Expected behavior

No crash.

Screenshots

Image

NuGet package version

WinUI 3 - Windows App SDK 1.6.3: 1.6.241114003

Windows version

Windows 11 (23H2): Build 22631

Additional context

This happens to UWP and WinUI3.
Repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds-triageIssue needs to be triaged by the area owners

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions