-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] <Services> 添加NavigationViewService.
- Loading branch information
Showing
12 changed files
with
207 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/HyPlayer.App/Interfaces/Services/INavigationViewService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.UI.Xaml.Controls; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace HyPlayer.Interfaces.Services; | ||
|
||
public interface INavigationViewService | ||
{ | ||
IList<object>? MenuItems | ||
{ | ||
get; | ||
} | ||
|
||
object? SettingsItem | ||
{ | ||
get; | ||
} | ||
|
||
void Initialize(NavigationView navigationView); | ||
|
||
void UnregisterEvents(); | ||
|
||
NavigationViewItem? GetSelectedItem(Type pageType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
using HyPlayer.Interfaces.Services; | ||
using HyPlayer.Extensions.Helpers; | ||
using HyPlayer.ViewModels; | ||
|
||
using Microsoft.UI.Xaml.Controls; | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace HyPlayer.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("SettingsPage"); | ||
} | ||
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using HyPlayer.Interfaces.ViewModels; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HyPlayer.ViewModels | ||
{ | ||
public partial class SettingsViewModel : ObservableRecipient, IScopedViewModel | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<local:SettingsPageBase | ||
x:Class="HyPlayer.Views.Pages.SettingsPage" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="using:HyPlayer.Views.Pages" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d" | ||
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> | ||
|
||
<Grid> | ||
|
||
</Grid> | ||
</local:SettingsPageBase> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using HyPlayer.Interfaces.Views; | ||
using HyPlayer.ViewModels; | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
using Microsoft.UI.Xaml.Controls.Primitives; | ||
using Microsoft.UI.Xaml.Data; | ||
using Microsoft.UI.Xaml.Input; | ||
using Microsoft.UI.Xaml.Media; | ||
using Microsoft.UI.Xaml.Navigation; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.InteropServices.WindowsRuntime; | ||
using Windows.Foundation; | ||
using Windows.Foundation.Collections; | ||
|
||
|
||
namespace HyPlayer.Views.Pages | ||
{ | ||
/// <summary> | ||
/// An empty page that can be used on its own or navigated to within a Frame. | ||
/// </summary> | ||
public sealed partial class SettingsPage : SettingsPageBase | ||
{ | ||
public SettingsPage() | ||
{ | ||
this.InitializeComponent(); | ||
} | ||
} | ||
|
||
public class SettingsPageBase : AppPageWithScopedViewModelBase<SettingsViewModel> | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters