question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FlyoutBase.ShowAttachedFlyout() throws exception on return to page

See original GitHub issue

Describe the bug

This is in a WinUI 3 (v1) app using a NavigationView to display various Page elements. One of the pages has a ListView which, in turn, has a flyout which opens when you select an item in the ListView:

<ListView x:Name="LocationsGrid" 
                 HorizontalAlignment="Left"
                 VerticalAlignment="Top"
                 ItemsSource="{x:Bind ViewModel.AllPoints}"
                 SelectedItem="{x:Bind ViewModel.SelectedPoint, Mode=TwoWay}"
                 SelectionChanged="LocationsGrid_OnSelectionChanged">

        <FlyoutBase.AttachedFlyout>
            <Flyout>
                <Grid>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="0">Message</TextBlock>

                    <TextBlock Grid.Row="0" Grid.Column="1"
                                       Text="{x:Bind ViewModel.SelectedPoint.DeviceLocation.TextMessage}"/>

                    <TextBlock Grid.Row="1" Grid.Column="0">Recipients</TextBlock>

                    <ListView Grid.Row="1" Grid.Column="1"
                                      ItemsSource="{x:Bind ViewModel.SelectedPoint.DeviceLocation.Recipients}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}"/>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

                </Grid>
            </Flyout>
        </FlyoutBase.AttachedFlyout>

The flyout is opened when an item is selected:

        private void LocationsGrid_OnSelectionChanged( object sender, SelectionChangedEventArgs e )
        {
            if( !ViewModel.SelectedPoint?.DeviceLocation.HasMessage ?? false )
                return;

            // have to wrap this in try/catch because when you return to the page
            // from another page it will throw an exception
            try
            {
                FlyoutBase.ShowAttachedFlyout( (FrameworkElement) sender );
            }
            catch
            {
                // ignored
            }
        }

Steps to reproduce the bug

This works correctly when you initially navigate to the page with the ListView and flyout. However, if you navigate to a different page and then return to the page with the ListView and flyout the select handler LocationsGrid_OnSelection is triggered (even though you haven’t yet selected anything) and, without the try/catch block in the handler, an exception is thrown.

The exception comes from somewhere inside the WinRT subsystem. When it is initially thrown it complains about some object not being in the correct (any?) visual tree. Unfortunately, by the time the exception propagates it gets turned into an “value is not in acceptable range” exception, without any of the original details (i.e., there is no inner exception; as an aside, I really, really wish the code base would stop throwing “useless” exception messages which contain no helpful identifying information).

Expected behavior

I shouldn’t have to handle the exception. In fact, the select handler shouldn’t even be triggered when I return to the page because I haven’t selected anything yet.

Screenshots

No response

NuGet package version

No response

Windows app type

  • UWP
  • Win32

Device form factor

Desktop

Windows version

Windows 11 (21H2): Build 22000

Additional context

Not sure about my answer on “Windows app type”. Here’s an extract from my csproj file:

<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>J4JSoftware.GPSLocator</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
markolbertcommented, Mar 24, 2022

I was able to determine the problem was caused by the flyout display logic (in the code behind) being triggered by the SelectionChanged event rather than the Tapped event.

SelectionChanged gets triggered, if you’ve previously selected something, whenever you navigate back to the page with the flyout (which makes sense in the context of wanting to maintain knowledge of which item has been selected between activations).

Switching to using the Tapped event solved the problem, since Tapped doesn’t retain state between activations of the page.

Contact me if you still need/want additional info but for now I’m closing the issue.

Thanx for your help.

0reactions
markolbertcommented, Mar 24, 2022

No, the SelectedItem and SelectedPoint should be the same. SelectedPoint is a viewmodel property and the viewmodel is a single instance so I don’t see how it could change.

  • Mark Sent from my iPhone

On Mar 23, 2022, at 3:05 PM, Dilip Ojha @.***> wrote:

@markolberthttps://github.com/markolbert Is your SelectedItemchanging upon return to the page? (The SelectedPoint that you bind to SelectedItem)

— Reply to this email directly, view it on GitHubhttps://github.com/microsoft/microsoft-ui-xaml/issues/6880#issuecomment-1076865394, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACFMTOVTXF2HDIEDRZ77JT3VBOIRDANCNFSM5ROTQBXA. You are receiving this because you were mentioned.Message ID: @.***>

Read more comments on GitHub >

github_iconTop Results From Across the Web

CommandBarFlyout keeps throwing a System. ...
You need to set the FlyoutBase.AttachedFlyout for the TextBlock and then call FlyoutBase.ShowAttachedFlyout method to show it.
Read more >
FlyoutBase.ShowAttachedFlyout(FrameworkElement) ...
Shows the flyout associated with the specified element, if any.
Read more >
ContentDialog Class (Windows.UI.Xaml.Controls)
Attempting to open two ContentDialogs will throw an exception, even if they are attempting to open in separate AppWindows. Control style and template....
Read more >
How to debug a task exception in Windows Store application
If the Facebook account is an administrator, a new app bar command Admin will be displayed. Click it to navigate to the admin/publish...
Read more >
C# – Ratish Philip's Blog - WordPress.com
For a PathKeyFrameAnimation, each keyframe value is a CompositionPath (yes, that is an exception to the “references are not animatable properties” rule).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found