Skip to content

Commit

Permalink
Add BackRequested for NavigationView
Browse files Browse the repository at this point in the history
  • Loading branch information
Lioncky committed Sep 15, 2024
1 parent d717da1 commit 4f8b84a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions WinUI3Localizer.SampleApp/ShellPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<NavigationView
x:Name="NavigationViewControl"
PaneDisplayMode="Top"
BackRequested = "NavigationViewControl_BackRequested"
SelectionChanged="NavigationView_SelectionChanged">
<NavigationView.MenuItems>
<NavigationViewItem
Expand Down
33 changes: 33 additions & 0 deletions WinUI3Localizer.SampleApp/ShellPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -21,6 +22,7 @@ public ShellPage()
.FirstOrDefault(x => x.Language == Localizer.Get().GetCurrentLanguage());

this.NavigationViewControl.Loaded += NavigationViewControl_Loaded;
this.ContentFrame.Navigated += On_Navigated;

LanguageDictionaryItems = Localizer
.Get()
Expand All @@ -42,7 +44,38 @@ private void NavigationViewControl_Loaded(object sender, RoutedEventArgs e)
Uids.SetUid(settingsItem, "MainWindow_NavigationView_Settings");
}
}
private void NavigationViewControl_BackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
{
this.ContentFrame.GoBack();
}
private void On_Navigated(object sender, NavigationEventArgs e)
{
this.NavigationViewControl.IsBackEnabled = this.ContentFrame.CanGoBack;

if (this.ContentFrame.SourcePageType == typeof(SettingsPage))
{
// SettingsItem is not part of NavView.MenuItems, and doesn't have a Tag.
this.NavigationViewControl.SelectedItem = (NavigationViewItem)this.NavigationViewControl.SettingsItem;

// We don't use Control.Header sofar
// NavigationViewControl.Header = "Settings";
}
else if (this.ContentFrame.SourcePageType != null)
{
// Select the nav view item that corresponds to the page being navigated to.
string? s = this.ContentFrame.SourcePageType.FullName;
string result = s?.Substring(s.LastIndexOf('.') + 1) ?? string.Empty;
if (result.Length > 0)
{
this.NavigationViewControl.SelectedItem = this.NavigationViewControl.MenuItems
.OfType<NavigationViewItem>()
.First(i => i.Tag.Equals(result));

// We don't use Control.Header sofar
//this.NavigationViewControl.Header = ((NavigationViewItem)this.NavigationViewControl.SelectedItem).Tag;
}
}
}
private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
if (args.IsSettingsSelected is true)
Expand Down

0 comments on commit 4f8b84a

Please sign in to comment.