microsoft / microsoft-ui-xaml

WinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.
MIT License
6.38k stars 683 forks source link

COMException when using `StackPanel` as `FlipView.ItemsPanel` in a `StackPanel` #8630

Open stephtr opened 1 year ago

stephtr commented 1 year ago

Describe the bug

When packing a FlipView into a StackPanel and setting FlipView.ItemsPanel to a StackPanel (see reproduction details below), the app crashes when the bound ItemsSource changes, in case both StackPanel's orientation are the same.

The COMException states:

System.Runtime.InteropServices.COMException
  HResult=0x80004005
  Message = Error HRESULT E_FAIL has been returned from a call to a COM component.
  Source = WinRT.Runtime
  Call stack:
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.System.Collections.Specialized.NotifyCollectionChangedEventHandler.NativeDelegateWrapper.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
   at System.Collections.ObjectModel.Collection`1.Add(T item)
   at App1.MainWindow.myButton_Click(Object sender, RoutedEventArgs e) in C:\Users\stroy\source\repos\App1\App1\MainWindow.xaml.cs:line 37
   at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.<GetEventInvoke>b__1_0(Object sender, RoutedEventArgs e)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

Steps to reproduce the bug

Create a new WinUI 3 desktop app with:

<StackPanel Orientation="Horizontal">
    <FlipView ItemsSource="{x:Bind VM.Items, Mode=OneWay}" x:Name="flip">
        <FlipView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel AreScrollSnapPointsRegular="False" Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </FlipView.ItemsPanel>
    </FlipView>
    <Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>

and

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
    }

    private ObservableCollection<string> Items = new();

    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        Items.Add("Hello World");
    }
}

leads to the COM exception.

Expected behavior

No response

Screenshots

No response

NuGet package version

None

Windows version

Windows Insider Build (xxxxx)

Additional context

Windows 11 build 23486, .NET 7.0, WindowsAppSDK 1.3.230602002

ranjeshj commented 9 months ago

@stephtr What is the scenario you are trying to achieve ?

stephtr commented 9 months ago

The idea was to have a FlipView filled with controls, where the amount of controls depends on the bound items. The hope was that WinUI would then automatically figure out the optimal height for the FlipView.

image image