Skip to content

Commit

Permalink
[WIP] [Refactor] 重构导航逻辑。
Browse files Browse the repository at this point in the history
  • Loading branch information
zhh135 committed Feb 1, 2024
1 parent 1072a10 commit 29e67a9
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/HyPlayer.App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<None Remove="Views\Pages\HomePage.xaml" />
<None Remove="Views\Pages\PlaylistPage.xaml" />
<None Remove="Views\Pages\SearchPage.xaml" />
<None Remove="Views\Pages\SettingsPage.xaml" />
<None Remove="Views\Pages\ShellPage.xaml" />
<None Remove="Views\Pages\UserPage.xaml" />
<None Remove="Views\Window\MainWindow.xaml" />
Expand Down Expand Up @@ -108,6 +109,9 @@
<None Update="Resources\Theme.xaml">
<Generator>MSBuild:Compile</Generator>
</None>
<Page Update="Views\Pages\SettingsPage.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\Controls\App\AppTitleBar.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down
1 change: 1 addition & 0 deletions src/HyPlayer.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public App()
Depository?.AddSingleton<IActivationService, ActivationService>();
Depository?.AddSingleton<IShellService, ShellService>();
Depository?.AddSingleton<IPageService, PageService>();
Depository?.AddSingleton<INavigationViewService, NavigationViewService>();
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/HyPlayer.App/Interfaces/Services/INavigationService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;

Expand All @@ -22,5 +22,7 @@ bool CanGoBack

bool NavigateTo(Page Page, object? parameter = null);

bool NavigateTo(string pageKey, object? parameter = null);

bool GoBack();
}
28 changes: 28 additions & 0 deletions src/HyPlayer.App/Interfaces/Services/INavigationViewService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HyPlayer.Interfaces.Services
{
interface INavigationViewService
{
IList<object>? MenuItems
{
get;
}

object? SettingsItem
{
get;
}

void Initialize(NavigationView navigationView);

void UnregisterEvents();

NavigationViewItem? GetSelectedItem(Type pageType);
}
}
2 changes: 1 addition & 1 deletion src/HyPlayer.App/Services/ActivationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task OnLaunchedAsync(object ActivateEventArgs)
Window MainWindow = WindowHelper.CreateWindow();
if (ActivatedEventArgs.Kind == ExtendedActivationKind.Launch)
{
App.GetService<INavigationService>().NavigateTo(typeof(Views.Pages.HomePage), ActivateEventArgs);
App.GetService<INavigationService>().NavigateTo(typeof(Views.Pages.ErrorPage), ActivateEventArgs);
}
MainWindow.Activate();
}
Expand Down
35 changes: 32 additions & 3 deletions src/HyPlayer.App/Services/NavigationService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using System;
Expand All @@ -8,6 +8,14 @@ namespace HyPlayer.Services
{
internal class NavigationService : INavigationService
{
private readonly IPageService _pageService;
private object? _lastParameterUsed;

public NavigationService(IPageService pageService)
{
_pageService = pageService;
}

public bool CanGoBack => App.contentFrame.CanGoBack;

public Frame? Frame => App.contentFrame;
Expand All @@ -24,7 +32,7 @@ public bool GoBack()
else return false;
}

public bool NavigateTo(Type Page, object parameter)
public bool NavigateTo(Type Page, object? parameter = null)
{
if (Frame != null)
{
Expand All @@ -36,7 +44,7 @@ public bool NavigateTo(Type Page, object parameter)
}
}

public bool NavigateTo(Page Page, object parameter)
public bool NavigateTo(Page Page, object? parameter = null)
{
if (Frame != null)
{
Expand All @@ -48,5 +56,26 @@ public bool NavigateTo(Page Page, object parameter)
return false;
}
}

public bool NavigateTo(string pageKey, object? parameter = null)
{
var pageType = _pageService.GetPageType(pageKey);

if (Frame != null && (Frame.Content?.GetType() != pageType || (parameter != null && !parameter.Equals(_lastParameterUsed))))
{

var navigated = Frame.Navigate(pageType, parameter);
if (navigated)
{
_lastParameterUsed = parameter;

}

return navigated;
}

return false;
}

}
}
66 changes: 66 additions & 0 deletions src/HyPlayer.App/Services/NavigationViewService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using HyPlayer.Helpers;
using HyPlayer.Interfaces.Services;
using HyPlayer.ViewModels;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HyPlayer.Services
{
class NavigationViewService : INavigationViewService
{
private readonly INavigationService _navigationService;
private readonly IPageService _pageService;
private NavigationView? _navigationView;

public NavigationViewService(INavigationService navigationService, IPageService pageService)
{
_navigationService = navigationService;
_pageService = pageService;
}

public IList<object>? MenuItems => _navigationView.MenuItems;

public object? SettingsItem => _navigationView.SettingsItem;

public NavigationViewItem? GetSelectedItem(Type pageType)
{
throw new NotImplementedException();
}

public void Initialize(NavigationView navigationView)
{
_navigationView = navigationView;
navigationView.ItemInvoked += OnItemInvoked;
navigationView.BackRequested += OnBackRequested;
}

public void UnregisterEvents()
{
throw new NotImplementedException();
}

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);
}
}
}
}
}
1 change: 1 addition & 0 deletions src/HyPlayer.App/Services/PageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public PageService()
ConfigurePage<SearchViewModel, SearchPage>();
ConfigurePage<NeteasePlaylistViewModel, PlaylistPage>();
ConfigurePage<UserViewModel, UserPage>();
ConfigurePage<SettingsViewModel, SettingsPage>();
}

public Type GetPageType(string key)
Expand Down
14 changes: 14 additions & 0 deletions src/HyPlayer.App/ViewModels/SettingsViewModel.cs
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 : ObservableObject, IScopedViewModel
{
}
}
9 changes: 7 additions & 2 deletions src/HyPlayer.App/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ namespace HyPlayer.ViewModels
{
public partial class ShellViewModel : ObservableObject, IScopedViewModel
{
private readonly INavigationViewService _navigationViewService;
private readonly INavigationService _navigationService;

[ObservableProperty] private AccountViewModel _accountViewModel;
public bool IsProgressBarVisible => App.GetService<IShellService>().IsProgressBarVisible;

public ShellViewModel(AccountViewModel accountViewModel)
{
public ShellViewModel(AccountViewModel accountViewModel, INavigationViewService navigationViewService, INavigationService navigationService)
{
_accountViewModel = accountViewModel;
_navigationViewService = navigationViewService;
_navigationService = navigationService;
}

[RelayCommand]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:base="using:HyPlayer.Views.Controls.Base"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="using:HyPlayer.Helpers"
xmlns:local="using:HyPlayer.Views.Controls.App"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
Expand All @@ -17,7 +18,6 @@
IsBackEnabled="True"
IsPaneOpen="{x:Bind IsPaneOpen}"
IsPaneToggleButtonVisible="True"
ItemInvoked="{x:Bind NavView_ItemInvoked}"
Loaded="NavView_Loaded"
Style="{StaticResource LeftNavViewDefaultStyle}">

Expand All @@ -28,7 +28,8 @@
<NavigationViewItem
x:Name="Home"
x:Uid="MainNav_Item_Home"
Tag="HomePage">
helpers:NavigationHelper.NavigateTo="HomePage"
Tag="HyPlayer.ViewModel.HomeViewModel">
<NavigationViewItem.Icon>
<FontIcon Glyph="&#xEA8A;" />
</NavigationViewItem.Icon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,10 @@ public DesktopNavigationView()
{
this.InitializeComponent();
HyPlayer.App.contentFrame = contentFrame;
HyPlayer.App.GetService<INavigationViewService>().Initialize(NavView);
}



private void NavView_ItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sender, Microsoft.UI.Xaml.Controls.NavigationViewItemInvokedEventArgs args)
{
var invokedItemTag = HyPlayer.App.GetService<IPageService>().GetPageType((args.InvokedItemContainer as NavigationViewItem)?.Tag?.ToString());
var invokedItemName = (args.InvokedItemContainer as NavigationViewItem)?.Content?.ToString();


HyPlayer.App.GetService<INavigationService>().NavigateTo(invokedItemTag);
// NavView.Header = invokedItemName;
}

private void NavView_Loaded(object sender, RoutedEventArgs e)
{
NavView.SelectedItem = Home;
Expand Down
15 changes: 15 additions & 0 deletions src/HyPlayer.App/Views/Pages/SettingsPage.xaml
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>
38 changes: 38 additions & 0 deletions src/HyPlayer.App/Views/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
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 HyPlayer.Interfaces.Views;
using HyPlayer.ViewModels;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

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>
{

}
}

0 comments on commit 29e67a9

Please sign in to comment.