[UWP][VS] NavigationView Example Frame not updating properly?

Hi, I’m trying to build the example from https://docs.microsoft.com/de-de/windows/uwp/design/controls-and-patterns/navigationview#code-example.

Here is my HomePage.xaml

<Page x:Class="AdminTools.HomePage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:muxc="using:Microsoft.UI.Xaml.Controls" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid> <muxc:NavigationView x:Name="NavView" Loaded="NavView_Loaded" ItemInvoked="NavView_ItemInvoked" BackRequested="NavView_BackRequested"> <muxc:NavigationView.MenuItems> <muxc:NavigationViewItem Tag="home" Icon="Home" Content="Home"/> <muxc:NavigationViewItemSeparator/> <muxc:NavigationViewItemHeader x:Name="MenuAppListHeading" Content="Admin Dev Tools"/> <muxc:NavigationViewItem Tag="apps" Content="Apps"> <muxc:NavigationViewItem.Icon> <FontIcon FontFamily="Segoe MDL2 Assets" Glyph=""/> </muxc:NavigationViewItem.Icon> </muxc:NavigationViewItem> <muxc:NavigationViewItem Tag="games" Content="Games"> <muxc:NavigationViewItem.Icon> <FontIcon FontFamily="Segoe MDL2 Assets" Glyph=""/> </muxc:NavigationViewItem.Icon> </muxc:NavigationViewItem> <muxc:NavigationViewItem Tag="music" Icon="Audio" Content="Music"/> </muxc:NavigationView.MenuItems> <muxc:NavigationView.AutoSuggestBox> <!-- See AutoSuggestBox documentation for more info about how to implement search. --> <AutoSuggestBox x:Name="NavViewSearchBox" QueryIcon="Find"/> </muxc:NavigationView.AutoSuggestBox> <ScrollViewer> <Frame x:Name="ContentFrame" Padding="12,0,12,24" IsTabStop="True" NavigationFailed="ContentFrame_NavigationFailed"/> </ScrollViewer> </muxc:NavigationView> <VisualStateManager.VisualStateGroups> <VisualStateGroup> <VisualState> <VisualState.StateTriggers> <AdaptiveTrigger MinWindowWidth="{x:Bind NavView.CompactModeThresholdWidth}"/> </VisualState.StateTriggers> <VisualState.Setters> <!-- Leave the next line for left-only navigation. --> <Setter Target="ContentFrame.Padding" Value="24,0,24,24"/> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> </Grid> </Page> 

And this is my HomePage.xaml.cs:

using System; using System.Diagnostics; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Animation; using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238 namespace AdminTools { using muxc = Microsoft.UI.Xaml.Controls; /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class HomePage : Page { public HomePage() { this.InitializeComponent(); } private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) { throw new Exception("Failed to load Page " + e.SourcePageType.FullName); } // List of ValueTuple holding the Navigation Tag and the relative Navigation Page private readonly List<(string Tag, Type Page)> _pages = new List<(string Tag, Type Page)> { ("home", typeof(HomePage)), ("apps", typeof(WebDevPage)), ("games", typeof(RDPPage)), ("music", typeof(ChatPage)), }; private bool isfirst; private void NavView_Loaded(object sender, RoutedEventArgs e) { // Add handler for ContentFrame navigation. ContentFrame.Navigated += On_Navigated; // NavView doesn't load any page by default, so load home page. NavView.SelectedItem = NavView.MenuItems[0]; isfirst = true; // Because we use ItemInvoked to navigate, we need to call Navigate // here to load the home page. NavView_Navigate("home", new EntranceNavigationTransitionInfo()); // Add keyboard accelerators for backwards navigation. var goBack = new KeyboardAccelerator { Key = VirtualKey.GoBack }; goBack.Invoked += BackInvoked; this.KeyboardAccelerators.Add(goBack); // ALT routes here var altLeft = new KeyboardAccelerator { Key = VirtualKey.Left, Modifiers = VirtualKeyModifiers.Menu }; altLeft.Invoked += BackInvoked; this.KeyboardAccelerators.Add(altLeft); } private void NavView_ItemInvoked(muxc.NavigationView sender, muxc.NavigationViewItemInvokedEventArgs args) { if (args.IsSettingsInvoked == true) { NavView_Navigate("settings", args.RecommendedNavigationTransitionInfo); } else if (args.InvokedItemContainer != null) { var navItemTag = args.InvokedItemContainer.Tag.ToString(); NavView_Navigate(navItemTag, args.RecommendedNavigationTransitionInfo); } } private void NavView_Navigate(string navItemTag, NavigationTransitionInfo transitionInfo) { Type _page = null; if (navItemTag == "settings") { _page = typeof(SettingsPage); } else { var item = _pages.First(p => p.Tag.Equals(navItemTag)); _page = item.Page; } // Get the page type before navigation so you can prevent duplicate // entries in the backstack. var preNavPageType = ContentFrame.CurrentSourcePageType; // Only navigate if the selected page isn't currently loaded. if (!(_page is null) && !Type.Equals(preNavPageType, _page) && !isfirst) { ContentFrame.Navigate(_page, null, transitionInfo); } } private void NavView_BackRequested(muxc.NavigationView sender, muxc.NavigationViewBackRequestedEventArgs args) { On_BackRequested(); } private void BackInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) { On_BackRequested(); args.Handled = true; } private bool On_BackRequested() { if (!ContentFrame.CanGoBack) return false; ContentFrame.GoBack(); return true; } private void On_Navigated(object sender, NavigationEventArgs e) { Type _page = null; NavView.IsBackEnabled = ContentFrame.CanGoBack; if (ContentFrame.SourcePageType == typeof(SettingsPage)) { // SettingsItem is not part of NavView.MenuItems, and doesn't have a Tag. NavView.SelectedItem = (muxc.NavigationViewItem)NavView.SettingsItem; NavView.Header = "Settings"; } else if (ContentFrame.SourcePageType != null) { var item = _pages.First(p => p.Page == e.SourcePageType); _page = item.Page; NavView.SelectedItem = NavView.MenuItems .OfType<muxc.NavigationViewItem>() .First(n => n.Tag.Equals(item.Tag)); NavView.Header = ((muxc.NavigationViewItem)NavView.SelectedItem)?.Content?.ToString(); } } } } 

Compilation works but as soon as I click on a NavView item, it doesn’t update the frame context with the associated page defined in the ValueTuple List. Instead it shows a blank page. No exceptions or crashes at all.

I’m new to c# and uwp, so I kindly ask you experts: where does my logic fail here?


submitted by /u/utf80
[link] [comments]

Leave a Reply