From 14a2af0d4670fe12469a33953e45bac67ffbc305 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Mon, 13 Nov 2023 16:24:03 -0600 Subject: [PATCH] feat: add NavigationView RegionAdapter --- e2e/Uno/HelloWorld/Views/Shell.xaml | 12 +++++- .../Regions/NavigationViewRegionAdapter.cs | 37 +++++++++++++++++++ .../PrismInitializationExtensions.cs | 3 ++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/Uno/Prism.Uno/Navigation/Regions/NavigationViewRegionAdapter.cs diff --git a/e2e/Uno/HelloWorld/Views/Shell.xaml b/e2e/Uno/HelloWorld/Views/Shell.xaml index 6875627f88..0b84db3bc1 100644 --- a/e2e/Uno/HelloWorld/Views/Shell.xaml +++ b/e2e/Uno/HelloWorld/Views/Shell.xaml @@ -32,7 +32,7 @@ - + + + + + + + + diff --git a/src/Uno/Prism.Uno/Navigation/Regions/NavigationViewRegionAdapter.cs b/src/Uno/Prism.Uno/Navigation/Regions/NavigationViewRegionAdapter.cs new file mode 100644 index 0000000000..12b24439f6 --- /dev/null +++ b/src/Uno/Prism.Uno/Navigation/Regions/NavigationViewRegionAdapter.cs @@ -0,0 +1,37 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; + +namespace Prism.Navigation.Regions; + +public sealed class NavigationViewRegionAdapter : RegionAdapterBase +{ + public NavigationViewRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) + : base(regionBehaviorFactory) + { + } + + protected override void Adapt(IRegion region, NavigationView regionTarget) + { + regionTarget.BackRequested += delegate + { + if (region.NavigationService.Journal.CanGoBack) + region.NavigationService.Journal.GoBack(); + }; + + regionTarget.SelectionChanged += delegate + { + if (regionTarget.SelectedItem is FrameworkElement item && item.Tag is string navigationTarget && !string.IsNullOrEmpty(navigationTarget)) + region.RequestNavigate(navigationTarget); + }; + + region.ActiveViews.CollectionChanged += delegate + { + regionTarget.Content = region.ActiveViews.FirstOrDefault(); + }; + } + + protected override IRegion CreateRegion() + { + return new SingleActiveRegion(); + } +} diff --git a/src/Wpf/Prism.Wpf/PrismInitializationExtensions.cs b/src/Wpf/Prism.Wpf/PrismInitializationExtensions.cs index 831c2733c7..4b9c54b074 100644 --- a/src/Wpf/Prism.Wpf/PrismInitializationExtensions.cs +++ b/src/Wpf/Prism.Wpf/PrismInitializationExtensions.cs @@ -67,6 +67,9 @@ internal static void RegisterDefaultRegionAdapterMappings(this RegionAdapterMapp regionAdapterMappings.RegisterMapping(); regionAdapterMappings.RegisterMapping(); regionAdapterMappings.RegisterMapping(); +#if HAS_WINUI + regionAdapterMappings.RegisterMapping(); +#endif } internal static void RunModuleManager(IContainerProvider containerProvider)