Skip to content
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
14 changes: 14 additions & 0 deletions PCL.Neo/Assets/Language/en-US.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<ResourceDictionary
xmlns="https://github.com/avaloniaui"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<!-- FormMain -->
<s:String x:Key="LangTitleDownload">Download</s:String>
<s:String x:Key="LangTitleHome">Home</s:String>
<s:String x:Key="LangTitleSetup">Settings</s:String>
<s:String x:Key="LangTitleLink">Multiplayer</s:String>
<s:String x:Key="LangTitleOther">More</s:String>
<s:String x:Key="LangNavigateBack">Back</s:String>

</ResourceDictionary>
1 change: 1 addition & 0 deletions PCL.Neo/Assets/Language/zh-CN.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<s:String x:Key="LangTitleSetup">设置</s:String>
<s:String x:Key="LangTitleLink">联机</s:String>
<s:String x:Key="LangTitleOther">更多</s:String>
<s:String x:Key="LangNavigateBack">返回</s:String>

</ResourceDictionary>
136 changes: 124 additions & 12 deletions PCL.Neo/Services/NavigationService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
using Microsoft.Extensions.DependencyInjection;
using PCL.Neo.ViewModels;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace PCL.Neo.Services;

public enum NavigationType
{
Forward,
Backward
}

public class NavigationEventArgs : EventArgs
{
public ViewModelBase? OldViewModel { get; }
public ViewModelBase? NewViewModel { get; }
public NavigationType NavigationType { get; }

public NavigationEventArgs(ViewModelBase? oldViewModel, ViewModelBase? newViewModel, NavigationType navigationType)
{
OldViewModel = oldViewModel;
NewViewModel = newViewModel;
NavigationType = navigationType;
}
}

public class NavigationService
{
public IServiceProvider ServiceProvider { get; init; }

public event Action<ViewModelBase?>? CurrentViewModelChanged;
public event Action<ViewModelBase?>? CurrentSubViewModelChanged;
public event Action<NavigationEventArgs>? Navigating;

// 导航历史记录
private readonly Stack<(Type ViewModelType, Type? SubViewModelType)> _navigationHistory = new();
// 最大历史记录数量
private const int MaxHistoryCount = 30;

private ViewModelBase? _currentViewModel;
public ViewModelBase? CurrentViewModel
Expand All @@ -20,6 +47,8 @@ protected set
{
if (value == _currentViewModel)
return;

var oldViewModel = _currentViewModel;
_currentViewModel = value;
CurrentViewModelChanged?.Invoke(value);
}
Expand All @@ -33,18 +62,52 @@ protected set
{
if (value == _currentSubViewModel)
return;

var oldSubViewModel = _currentSubViewModel;
_currentSubViewModel = value;
CurrentSubViewModelChanged?.Invoke(value);
}
}

public bool CanGoBack => _navigationHistory.Count > 0;

public NavigationService(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}

public virtual T Goto<T>() where T : ViewModelBase
{
var oldViewModel = CurrentViewModel;
var oldSubViewModel = CurrentSubViewModel;

// 保存当前状态到历史记录
if (CurrentViewModel != null)
{
_navigationHistory.Push((CurrentViewModel.GetType(), CurrentSubViewModel?.GetType()));

// 限制历史记录数量
if (_navigationHistory.Count > MaxHistoryCount)
{
var tempStack = new Stack<(Type, Type?)>();
var count = 0;

while (_navigationHistory.Count > 0 && count < MaxHistoryCount)
{
tempStack.Push(_navigationHistory.Pop());
count++;
}

_navigationHistory.Clear();
while (tempStack.Count > 0)
{
_navigationHistory.Push(tempStack.Pop());
}
}
}

T targetVm;

if (typeof(T).GetCustomAttribute<DefaultSubViewModelAttribute>() is { } dsvm)
{
var vm = CurrentViewModel as T;
Expand All @@ -55,10 +118,9 @@ public virtual T Goto<T>() where T : ViewModelBase
}
if (CurrentSubViewModel?.GetType() != dsvm.SubViewModel)
CurrentSubViewModel = ServiceProvider.GetRequiredService(dsvm.SubViewModel) as ViewModelBase;
return vm;
targetVm = vm;
}

if (typeof(T).GetCustomAttribute<SubViewModelOfAttribute>() is { } svmo)
else if (typeof(T).GetCustomAttribute<SubViewModelOfAttribute>() is { } svmo)
{
var subVm = CurrentSubViewModel as T;
if (CurrentViewModel?.GetType() != svmo.ParentViewModel)
Expand All @@ -68,16 +130,66 @@ public virtual T Goto<T>() where T : ViewModelBase
subVm = ServiceProvider.GetRequiredService<T>();
CurrentSubViewModel = subVm;
}
return subVm;
targetVm = subVm;
}

var targetVm = CurrentViewModel?.GetType() != typeof(T) || CurrentSubViewModel?.GetType() != typeof(T)
? ServiceProvider.GetRequiredService<T>()
: (T)CurrentViewModel;
if (CurrentViewModel?.GetType() != typeof(T))
CurrentViewModel = targetVm;
if (CurrentSubViewModel?.GetType() != typeof(T))
CurrentSubViewModel = targetVm;
else
{
targetVm = CurrentViewModel?.GetType() != typeof(T) || CurrentSubViewModel?.GetType() != typeof(T)
? ServiceProvider.GetRequiredService<T>()
: (T)CurrentViewModel;
if (CurrentViewModel?.GetType() != typeof(T))
CurrentViewModel = targetVm;
if (CurrentSubViewModel?.GetType() != typeof(T))
CurrentSubViewModel = targetVm;
}

// 触发导航事件
Navigating?.Invoke(new NavigationEventArgs(oldViewModel, CurrentViewModel, NavigationType.Forward));

return targetVm;
}

/// <summary>
/// 返回上一个导航状态
/// </summary>
/// <returns>是否成功返回</returns>
public bool GoBack()
{
if (!CanGoBack)
return false;

var oldViewModel = CurrentViewModel;
var oldSubViewModel = CurrentSubViewModel;

var (viewModelType, subViewModelType) = _navigationHistory.Pop();

// 恢复主视图
if (viewModelType != null)
{
CurrentViewModel = ServiceProvider.GetRequiredService(viewModelType) as ViewModelBase;
}

// 恢复子视图
if (subViewModelType != null)
{
CurrentSubViewModel = ServiceProvider.GetRequiredService(subViewModelType) as ViewModelBase;
}
else
{
CurrentSubViewModel = null;
}

// 触发导航事件
Navigating?.Invoke(new NavigationEventArgs(oldViewModel, CurrentViewModel, NavigationType.Backward));

return true;
}

/// <summary>
/// 清空导航历史
/// </summary>
public void ClearHistory()
{
_navigationHistory.Clear();
}
}
23 changes: 23 additions & 0 deletions PCL.Neo/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
private ViewModelBase? _currentViewModel;
[ObservableProperty]
private ViewModelBase? _currentSubViewModel;

[ObservableProperty]
private bool _canGoBack;

// 为了设计时的 DataContext
public MainWindowViewModel()
{
throw new System.NotImplementedException();
}
public MainWindowViewModel(Window window)

Check warning on line 37 in PCL.Neo/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / build-AppImage

Non-nullable property 'NavigationService' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 37 in PCL.Neo/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / build-AppImage

Non-nullable property 'NavigationService' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 37 in PCL.Neo/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / build-WinExe

Non-nullable property 'NavigationService' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 37 in PCL.Neo/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / build-WinExe

Non-nullable property 'NavigationService' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 37 in PCL.Neo/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / build-MacOsApp

Non-nullable property 'NavigationService' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 37 in PCL.Neo/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / build-MacOsApp

Non-nullable property 'NavigationService' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
this._window = window;
}
Expand All @@ -41,19 +44,29 @@
this.NavigationService.CurrentViewModelChanged += x =>
{
CurrentViewModel = x;
CanGoBack = NavigationService.CanGoBack;
switch (x)
{
case HomeViewModel:
IsNavBtn1Checked = true;
IsNavBtn2Checked = false;
IsNavBtn3Checked = false;
IsNavBtn4Checked = false;
IsNavBtn5Checked = false;
break;
case DownloadViewModel:
IsNavBtn1Checked = false;
IsNavBtn2Checked = true;
IsNavBtn3Checked = false;
IsNavBtn4Checked = false;
IsNavBtn5Checked = false;
break;
}
};
this.NavigationService.CurrentSubViewModelChanged += x =>
{
CurrentSubViewModel = x;
CanGoBack = NavigationService.CanGoBack;
};
this.NavigationService.Goto<HomeViewModel>();
}
Expand All @@ -69,6 +82,16 @@
{
this.NavigationService.Goto<DownloadViewModel>();
}

[RelayCommand]
public void GoBack()
{
if (NavigationService.CanGoBack)
{
NavigationService.GoBack();
CanGoBack = NavigationService.CanGoBack;
}
}

public void Close()
{
Expand Down
Loading
Loading