mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
Add chapter 10 sample project
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
using TemplateStudioSampleApp.Contracts.Services;
|
||||
using TemplateStudioSampleApp.Helpers;
|
||||
using TemplateStudioSampleApp.ViewModels;
|
||||
|
||||
namespace TemplateStudioSampleApp.Services;
|
||||
|
||||
public class NavigationViewService : INavigationViewService
|
||||
{
|
||||
private readonly INavigationService _navigationService;
|
||||
|
||||
private readonly IPageService _pageService;
|
||||
|
||||
private NavigationView? _navigationView;
|
||||
|
||||
public IList<object>? MenuItems => _navigationView?.MenuItems;
|
||||
|
||||
public object? SettingsItem => _navigationView?.SettingsItem;
|
||||
|
||||
public NavigationViewService(INavigationService navigationService, IPageService pageService)
|
||||
{
|
||||
_navigationService = navigationService;
|
||||
_pageService = pageService;
|
||||
}
|
||||
|
||||
[MemberNotNull(nameof(_navigationView))]
|
||||
public void Initialize(NavigationView navigationView)
|
||||
{
|
||||
_navigationView = navigationView;
|
||||
_navigationView.BackRequested += OnBackRequested;
|
||||
_navigationView.ItemInvoked += OnItemInvoked;
|
||||
}
|
||||
|
||||
public void UnregisterEvents()
|
||||
{
|
||||
if (_navigationView != null)
|
||||
{
|
||||
_navigationView.BackRequested -= OnBackRequested;
|
||||
_navigationView.ItemInvoked -= OnItemInvoked;
|
||||
}
|
||||
}
|
||||
|
||||
public NavigationViewItem? GetSelectedItem(Type pageType)
|
||||
{
|
||||
if (_navigationView != null)
|
||||
{
|
||||
return GetSelectedItem(_navigationView.MenuItems, pageType) ?? GetSelectedItem(_navigationView.FooterMenuItems, pageType);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void OnBackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args) => _navigationService.GoBack();
|
||||
|
||||
private void OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
|
||||
{
|
||||
if (args.IsSettingsInvoked)
|
||||
{
|
||||
_navigationService.NavigateTo(typeof(SettingsViewModel).FullName!);
|
||||
}
|
||||
else
|
||||
{
|
||||
var selectedItem = args.InvokedItemContainer as NavigationViewItem;
|
||||
|
||||
if (selectedItem?.GetValue(NavigationHelper.NavigateToProperty) is string pageKey)
|
||||
{
|
||||
_navigationService.NavigateTo(pageKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private NavigationViewItem? GetSelectedItem(IEnumerable<object> menuItems, Type pageType)
|
||||
{
|
||||
foreach (var item in menuItems.OfType<NavigationViewItem>())
|
||||
{
|
||||
if (IsMenuItemForPageType(item, pageType))
|
||||
{
|
||||
return item;
|
||||
}
|
||||
|
||||
var selectedChild = GetSelectedItem(item.MenuItems, pageType);
|
||||
if (selectedChild != null)
|
||||
{
|
||||
return selectedChild;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private bool IsMenuItemForPageType(NavigationViewItem menuItem, Type sourcePageType)
|
||||
{
|
||||
if (menuItem.GetValue(NavigationHelper.NavigateToProperty) is string pageKey)
|
||||
{
|
||||
return _pageService.GetPageType(pageKey) == sourcePageType;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user