From d0e799b8edaf7378d38a7ef8bf71311dda29e262 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 15 May 2025 20:16:50 +0800 Subject: [PATCH] Fix double navigation issue --- .../SettingPages/Views/SettingsPaneAbout.xaml.cs | 10 +++++----- .../SettingPages/Views/SettingsPaneGeneral.xaml.cs | 10 +++++----- .../SettingPages/Views/SettingsPaneHotkey.xaml.cs | 10 +++++----- .../Views/SettingsPanePluginStore.xaml.cs | 10 +++++----- .../SettingPages/Views/SettingsPanePlugins.xaml.cs | 10 +++++----- .../SettingPages/Views/SettingsPaneProxy.xaml.cs | 10 +++++----- .../SettingPages/Views/SettingsPaneTheme.xaml.cs | 10 +++++----- Flow.Launcher/SettingWindow.xaml.cs | 11 ++++++++--- Flow.Launcher/ViewModel/SettingWindowViewModel.cs | 8 +++++--- 9 files changed, 48 insertions(+), 41 deletions(-) diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs index 00426546a..47532b243 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs @@ -8,24 +8,24 @@ namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPaneAbout { private SettingsPaneAboutViewModel _viewModel = null!; - private SettingWindowViewModel _settingViewModel = null; + private readonly SettingWindowViewModel _settingViewModel = Ioc.Default.GetRequiredService(); protected override void OnNavigatedTo(NavigationEventArgs e) { + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPaneAbout); + // If the navigation is not triggered by button click, view model will be null again if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); - _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; } if (!IsInitialized) { InitializeComponent(); } - // Sometimes the navigation is not triggered by button click, - // so we need to reset the page type - _settingViewModel.PageType = typeof(SettingsPaneAbout); base.OnNavigatedTo(e); } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs index 524e4f5f5..753cb7b0e 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs @@ -8,24 +8,24 @@ namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPaneGeneral { private SettingsPaneGeneralViewModel _viewModel = null!; - private SettingWindowViewModel _settingViewModel = null; + private readonly SettingWindowViewModel _settingViewModel = Ioc.Default.GetRequiredService(); protected override void OnNavigatedTo(NavigationEventArgs e) { + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPaneGeneral); + // If the navigation is not triggered by button click, view model will be null again if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); - _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; } if (!IsInitialized) { InitializeComponent(); } - // Sometimes the navigation is not triggered by button click, - // so we need to reset the page type - _settingViewModel.PageType = typeof(SettingsPaneGeneral); base.OnNavigatedTo(e); } } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs index 53a639aa5..202869bc5 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs @@ -8,24 +8,24 @@ namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPaneHotkey { private SettingsPaneHotkeyViewModel _viewModel = null!; - private SettingWindowViewModel _settingViewModel = null; + private readonly SettingWindowViewModel _settingViewModel = Ioc.Default.GetRequiredService(); protected override void OnNavigatedTo(NavigationEventArgs e) { + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPaneHotkey); + // If the navigation is not triggered by button click, view model will be null again if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); - _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; } if (!IsInitialized) { InitializeComponent(); } - // Sometimes the navigation is not triggered by button click, - // so we need to reset the page type - _settingViewModel.PageType = typeof(SettingsPaneHotkey); base.OnNavigatedTo(e); } } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs index 34cd33ec1..c0a77957a 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs @@ -11,15 +11,18 @@ namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPanePluginStore { private SettingsPanePluginStoreViewModel _viewModel = null!; - private SettingWindowViewModel _settingViewModel = null; + private readonly SettingWindowViewModel _settingViewModel = Ioc.Default.GetRequiredService(); protected override void OnNavigatedTo(NavigationEventArgs e) { + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPanePluginStore); + // If the navigation is not triggered by button click, view model will be null again if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); - _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; } if (!IsInitialized) @@ -27,9 +30,6 @@ public partial class SettingsPanePluginStore InitializeComponent(); } _viewModel.PropertyChanged += ViewModel_PropertyChanged; - // Sometimes the navigation is not triggered by button click, - // so we need to reset the page type - _settingViewModel.PageType = typeof(SettingsPanePluginStore); base.OnNavigatedTo(e); } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs index 94e0d24a2..f486a3443 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs @@ -11,15 +11,18 @@ namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPanePlugins { private SettingsPanePluginsViewModel _viewModel = null!; - private SettingWindowViewModel _settingViewModel = null; + private readonly SettingWindowViewModel _settingViewModel = Ioc.Default.GetRequiredService(); protected override void OnNavigatedTo(NavigationEventArgs e) { + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPanePlugins); + // If the navigation is not triggered by button click, view model will be null again if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); - _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; } if (!IsInitialized) @@ -27,9 +30,6 @@ public partial class SettingsPanePlugins InitializeComponent(); } _viewModel.PropertyChanged += ViewModel_PropertyChanged; - // Sometimes the navigation is not triggered by button click, - // so we need to reset the page type - _settingViewModel.PageType = typeof(SettingsPanePlugins); base.OnNavigatedTo(e); } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs index aa47383e8..3e617229d 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs @@ -8,24 +8,24 @@ namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPaneProxy { private SettingsPaneProxyViewModel _viewModel = null!; - private SettingWindowViewModel _settingViewModel = null; + private readonly SettingWindowViewModel _settingViewModel = Ioc.Default.GetRequiredService(); protected override void OnNavigatedTo(NavigationEventArgs e) { + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPaneProxy); + // If the navigation is not triggered by button click, view model will be null again if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); - _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; } if (!IsInitialized) { InitializeComponent(); } - // Sometimes the navigation is not triggered by button click, - // so we need to reset the page type - _settingViewModel.PageType = typeof(SettingsPaneProxy); base.OnNavigatedTo(e); } } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs index 215a8c70c..170003994 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs @@ -8,24 +8,24 @@ namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPaneTheme { private SettingsPaneThemeViewModel _viewModel = null!; - private SettingWindowViewModel _settingViewModel = null; + private readonly SettingWindowViewModel _settingViewModel = Ioc.Default.GetRequiredService(); protected override void OnNavigatedTo(NavigationEventArgs e) { + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPaneTheme); + // If the navigation is not triggered by button click, view model will be null again if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); - _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; } if (!IsInitialized) { InitializeComponent(); } - // Sometimes the navigation is not triggered by button click, - // so we need to reset the page type - _settingViewModel.PageType = typeof(SettingsPaneTheme); base.OnNavigatedTo(e); } } diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index d8444d45d..ebbb38414 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -241,6 +241,7 @@ public partial class SettingWindow { if (args.IsSettingsSelected) { + _viewModel.SetPageType(typeof(SettingsPaneGeneral)); ContentFrame.Navigate(typeof(SettingsPaneGeneral)); } else @@ -263,8 +264,11 @@ public partial class SettingWindow nameof(About) => typeof(SettingsPaneAbout), _ => typeof(SettingsPaneGeneral) }; - _viewModel.SetPageType(pageType); - ContentFrame.Navigate(pageType); + // Only navigate if the page type changes to fix navigation forward/back issue + if (_viewModel.SetPageType(pageType)) + { + ContentFrame.Navigate(pageType); + } } } @@ -282,7 +286,8 @@ public partial class SettingWindow private void ContentFrame_Loaded(object sender, RoutedEventArgs e) { - NavView.SelectedItem ??= NavView.MenuItems[0]; /* Set First Page */ + _viewModel.SetPageType(null); // Set page type to null so that NavigationView_SelectionChanged can navigate the frame + NavView.SelectedItem = NavView.MenuItems[0]; /* Set First Page */ } #endregion diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index 5b130eb6d..1134a81b8 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -1,7 +1,6 @@ using System; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; -using Flow.Launcher.SettingPages.Views; namespace Flow.Launcher.ViewModel; @@ -14,12 +13,15 @@ public partial class SettingWindowViewModel : BaseModel _settings = settings; } - public void SetPageType(Type pageType) + public bool SetPageType(Type pageType) { + if (_pageType == pageType) return false; + _pageType = pageType; + return true; } - private Type _pageType = typeof(SettingsPaneGeneral); + private Type _pageType = null; public Type PageType { get => _pageType;