Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NavigationView RegionAdapter #3000

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions e2e/Uno/HelloWorld/Views/Shell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</DataTemplate>
</toolkit:ExtendedSplashScreen.LoadingContentTemplate>
<Grid>
<Grid.RowDefinitions>
<!--<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
Expand All @@ -48,7 +48,15 @@
<Button Command="{Binding NavigateCommand}" CommandParameter="ViewB">Navigate to View B</Button>
</StackPanel>
</StackPanel>
<ContentControl Grid.Row="1" pr:RegionManager.RegionName="ContentRegion" />
<ContentControl Grid.Row="1" pr:RegionManager.RegionName="ContentRegion" />-->
<NavigationView pr:RegionManager.RegionName="ContentRegion"
IsSettingsVisible="false">
<NavigationView.MenuItems>
<NavigationViewItem Content="View A" Tag="ViewA" />
<NavigationViewItem Content="View B" Tag="ViewB" />
</NavigationView.MenuItems>
</NavigationView>

</Grid>
</toolkit:ExtendedSplashScreen>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Prism.Navigation.Regions;

public sealed class NavigationViewRegionAdapter : RegionAdapterBase<NavigationView>
{
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();
}
}
3 changes: 3 additions & 0 deletions src/Wpf/Prism.Wpf/PrismInitializationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ internal static void RegisterDefaultRegionAdapterMappings(this RegionAdapterMapp
regionAdapterMappings.RegisterMapping<Selector, SelectorRegionAdapter>();
regionAdapterMappings.RegisterMapping<ItemsControl, ItemsControlRegionAdapter>();
regionAdapterMappings.RegisterMapping<ContentControl, ContentControlRegionAdapter>();
#if HAS_WINUI
regionAdapterMappings.RegisterMapping<NavigationView, NavigationViewRegionAdapter>();
#endif
}

internal static void RunModuleManager(IContainerProvider containerProvider)
Expand Down
Loading