mirror of
https://github.com/real-zony/ZonyLrcToolsX.git
synced 2026-11-25 04:17:46 +08:00
refactor(desktop): 页面切换统一走 INavigationService
侧栏原先是在 MainWindow.axaml.cs 里用一个针对 ListBox.SelectedIndex 的 switch 接起来的,直接调 App.Services.GetRequiredService<...> 从 code-behind 赋页面。 NavigationService 存在、已注册,却从没被用过。 后果:首页的深层链接无法改变侧栏选中项;新增一个页面要同时改 view model 和 code-behind;每个页面即使该是单例也会被重新构造。 现在: - INavigationService 增加 NavigateRoot<T>(),用于"跳到顶层页面"(清空回退栈), 与已有的 NavigateTo<T>()、GoBack() 并存。 - MainWindowViewModel 订阅 NavigationChanged,把当前 view model 反映射成侧栏 索引,因此从首页卡片跳转时侧栏高亮会跟着走。 - MainWindow.axaml.cs 基本清空;ListBox 绑索引,导航由 view model 发起。 这个提交里两处对话框服务的新增是给后面页面用的: - ShowFilesPickerAsync —— 多选,解密页需要 - ShowQrCodeLoginDialogAsync —— 把网易云二维码对话框作为模态窗口承载 - ShowMessageAsync 补进接口。它早已实现、也早已被调用,只是没写进契约。
This commit is contained in:
1 parent
9cda87dbfe
commit
216ea4e0c4
8 files changed
+239
-46
No files matched your search
@@ -9,6 +9,7 @@ public partial class MainWindowViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IThemeService _themeService;
|
||||
private readonly IUILocalizationService _localization;
|
||||
private readonly INavigationService _navigation;
|
||||
|
||||
[ObservableProperty]
|
||||
private ViewModelBase? _currentPage;
|
||||
@@ -27,6 +28,7 @@ public partial class MainWindowViewModel : ViewModelBase
|
||||
public string NavHome => _localization["Nav_Home"];
|
||||
public string NavLyricsDownload => _localization["Nav_LyricsDownload"];
|
||||
public string NavAlbumDownload => _localization["Nav_AlbumDownload"];
|
||||
public string NavDecrypt => _localization["Nav_Decrypt"];
|
||||
public string NavSettings => _localization["Nav_Settings"];
|
||||
public string HomeDescription => _localization["Home_Description"];
|
||||
|
||||
@@ -37,25 +39,87 @@ public partial class MainWindowViewModel : ViewModelBase
|
||||
public MainWindowViewModel(
|
||||
IThemeService themeService,
|
||||
IUILocalizationService localization,
|
||||
HomeViewModel homeViewModel)
|
||||
INavigationService navigation)
|
||||
{
|
||||
_themeService = themeService;
|
||||
_localization = localization;
|
||||
_navigation = navigation;
|
||||
|
||||
// Subscribe to language changes
|
||||
_localization.LanguageChanged += OnLanguageChanged;
|
||||
|
||||
// Set initial page
|
||||
CurrentPage = homeViewModel;
|
||||
SelectedNavigationIndex = 0;
|
||||
// The navigation service is the single owner of "which page is showing".
|
||||
_navigation.NavigationChanged += OnNavigationChanged;
|
||||
|
||||
NavigateToIndex(0);
|
||||
}
|
||||
|
||||
// Guards against the re-entrant loop "index changed -> navigate -> navigation changed -> index changed".
|
||||
private bool _isSyncingNavigation;
|
||||
|
||||
partial void OnSelectedNavigationIndexChanged(int value)
|
||||
{
|
||||
if (_isSyncingNavigation)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NavigateToIndex(value);
|
||||
}
|
||||
|
||||
private void NavigateToIndex(int index)
|
||||
{
|
||||
// NavigateRoot keeps the stack depth at one: sidebar switching is not a back-navigable
|
||||
// action, so clicking around must not grow history.
|
||||
CurrentPage = index switch
|
||||
{
|
||||
1 => _navigation.NavigateRoot<LyricsDownloadViewModel>(),
|
||||
2 => _navigation.NavigateRoot<AlbumDownloadViewModel>(),
|
||||
3 => _navigation.NavigateRoot<DecryptViewModel>(),
|
||||
4 => _navigation.NavigateRoot<SettingsViewModel>(),
|
||||
_ => _navigation.NavigateRoot<HomeViewModel>()
|
||||
};
|
||||
}
|
||||
|
||||
private void OnNavigationChanged(object? sender, ViewModelBase viewModel)
|
||||
{
|
||||
CurrentPage = viewModel;
|
||||
|
||||
// The home page quick actions navigate directly, so the sidebar has to follow them.
|
||||
var index = IndexOf(viewModel);
|
||||
if (index < 0 || index == SelectedNavigationIndex)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isSyncingNavigation = true;
|
||||
try
|
||||
{
|
||||
SelectedNavigationIndex = index;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isSyncingNavigation = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static int IndexOf(ViewModelBase viewModel) => viewModel switch
|
||||
{
|
||||
LyricsDownloadViewModel => 1,
|
||||
AlbumDownloadViewModel => 2,
|
||||
DecryptViewModel => 3,
|
||||
SettingsViewModel => 4,
|
||||
HomeViewModel => 0,
|
||||
_ => -1
|
||||
};
|
||||
|
||||
private void OnLanguageChanged(object? sender, EventArgs e)
|
||||
{
|
||||
// Notify all localized properties have changed
|
||||
OnPropertyChanged(nameof(NavHome));
|
||||
OnPropertyChanged(nameof(NavLyricsDownload));
|
||||
OnPropertyChanged(nameof(NavAlbumDownload));
|
||||
OnPropertyChanged(nameof(NavDecrypt));
|
||||
OnPropertyChanged(nameof(NavSettings));
|
||||
OnPropertyChanged(nameof(HomeDescription));
|
||||
OnPropertyChanged(nameof(ThemeButtonText));
|
||||
|
||||
Reference in new issue
Block a user