Fix double navigation issue

This commit is contained in:
Jack251970 2025-05-15 20:16:50 +08:00
parent 5b17fad67c
commit d0e799b8ed
9 changed files with 48 additions and 41 deletions

View file

@ -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<SettingWindowViewModel>();
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<SettingsPaneAboutViewModel>();
_settingViewModel = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
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);
}

View file

@ -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<SettingWindowViewModel>();
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<SettingsPaneGeneralViewModel>();
_settingViewModel = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
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);
}
}

View file

@ -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<SettingWindowViewModel>();
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<SettingsPaneHotkeyViewModel>();
_settingViewModel = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
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);
}
}

View file

@ -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<SettingWindowViewModel>();
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<SettingsPanePluginStoreViewModel>();
_settingViewModel = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
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);
}

View file

@ -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<SettingWindowViewModel>();
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<SettingsPanePluginsViewModel>();
_settingViewModel = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
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);
}

View file

@ -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<SettingWindowViewModel>();
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<SettingsPaneProxyViewModel>();
_settingViewModel = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
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);
}
}

View file

@ -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<SettingWindowViewModel>();
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<SettingsPaneThemeViewModel>();
_settingViewModel = Ioc.Default.GetRequiredService<SettingWindowViewModel>();
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);
}
}

View file

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

View file

@ -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;