FlyoutBase.ShowAttachedFlyout() throws exception on return to page
See original GitHub issueDescribe 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:
- Created a year ago
- Comments:5 (2 by maintainers)

Top Related StackOverflow Question
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.
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.
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: @.***>