microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

Poor exception handling when setting wrong connected animtation

Open dotMorten opened this issue 6 months ago • 5 comments

Describe the bug

If I call ListView.PrepareConnectedAnimation with a misspelled elementName or an incorrect item that doesn't exist in the listview, an unhelpful exception "Value does not fall in the expected range" message, but even worse the breakpoint is not hit where the exception occurred, ends up in the unhandledexception handler.

This is one of these annoying behaviors that makes working with WinUI really hard and frustrating because we don't get helpful errors and breakpoints where the errors occur.

Steps to reproduce the bug

  1. Add the following XAML to your page:
<ListView ItemClick="ListView_ItemClick" x:Name="MyListView" SelectionMode="None" IsItemClickEnabled="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" x:Name="Title" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
  1. In code-behind add the following code:
        public MainWindow()
        {
            this.InitializeComponent();
            MyListView.ItemsSource = new string[] { "Title 1", "Title 2", "Title 3" };
        }

        private void ListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            // Throws because "WrongTitle" doesn't exist in xaml
            ((ListView)sender)?.PrepareConnectedAnimation("ForwardAnimation", e.ClickedItem, "WrongTitle");
            // Throws because "ABC" isn't a valid item in the list
            ((ListView)sender)?.PrepareConnectedAnimation("ForwardAnimation", "ABC", "Title");
            // Works:
            ((ListView)sender)?.PrepareConnectedAnimation("ForwardAnimation", e.ClickedItem, "Title");
        }
  1. Run the sample and observe the unhandled exception, unhelpful error message and the wrong location for the breakpoint.

Expected behavior

Error message should indicate that the "WrongTitle" object doesn't exist (first example), or that the provided item isn't a valid item in the Items collection of the listview (second example); Furthermore Visual Studio should be breaking at this line of code, and not go to the exception handler.

Screenshots

Notice how in the exception message the actual callstack is there, but in the callstack window we're in a completely different place: Image

NuGet package version

WinUI 3 - Windows App SDK 1.7.2: 1.7.250513003

Windows version

Windows 11 (24H2): Build 26100

Additional context

No response

dotMorten avatar Jun 05 '25 19:06 dotMorten

Also there is invalid argument exception upon xaml loading, when it can have thousands of control property that could go wrong. And I will have to use “binary search” to comment out elements to figure out.

HO-COOH avatar Jun 06 '25 09:06 HO-COOH

@HO-COOH I’d encourage you to log specific reproducers of these so we can help them get all these razor cuts addressed, instead of adding unrelated venting to other issues 😉

dotMorten avatar Jun 06 '25 15:06 dotMorten

@HO-COOH I’d encourage you to log specific reproducers of these so we can help them get all these razor cuts addressed, instead of adding unrelated venting to other issues 😉

Yes, please do and tag me in any issues you create that relate to unhelpful error messages.

RDMacLachlan avatar Jun 06 '25 17:06 RDMacLachlan

Hello @dotMorten, thanks for creating this thread, did you have WinUI binaries' symbols loaded, and native debugging enabled when you were debugging? I had enabled native debugging first and loaded symbols for WinUI dlls: Image

I see that now output window shows that it encountered E_INVALIDARGS ~

Image

And doing F5 when we are in Unhandled exception, callstack gets better ~

Image

Although, it doesn't say which one of the two params is invalid, but this should be good early, what's your thought on this?

Hemantxk avatar Jun 11 '25 13:06 Hemantxk

@Hemantxk the error is in the managed code so I shouldn’t have to enable native debugging. I’m aware of the extra steps you can do to try and troubleshoot this but this issue was logged to help improve the awful developer experience. In this case the breakpoint is in the wrong place and the error message isn’t helpful. This leads to a huge increase spent on debugging time and makes working with WinUI a painful and inefficient chore.

dotMorten avatar Jun 11 '25 13:06 dotMorten

@dotMorten, agreed that the current developer experience requires improvement.

We'll try updating our documentation to recommend enabling native debugging for non-managed code scenarios and ensuring that WinUI symbols are properly loaded for effective troubleshooting.

zinniaa avatar Jun 20 '25 11:06 zinniaa