fix(SettingsWindow): 修复导航逻辑中的重复导航问题

添加_isNavigating标志位防止在导航过程中重复触发导航事件
This commit is contained in:
PrefacedCorg
2026-04-21 01:02:22 +08:00
parent a57e0496ee
commit cab703b98e
@@ -27,6 +27,8 @@ namespace Ink_Canvas.Windows.SettingsViews
// 标记窗口是否曾经最大化过 // 标记窗口是否曾经最大化过
private bool _wasMaximized = false; private bool _wasMaximized = false;
private bool _isNavigating = false;
public SettingsWindow() public SettingsWindow()
{ {
InitializeComponent(); InitializeComponent();
@@ -208,7 +210,11 @@ namespace Ink_Canvas.Windows.SettingsViews
#region #region
private void OnNavigationViewSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) private void OnNavigationViewSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{ {
// 处理自带的设置项导航 if (_isNavigating)
{
return;
}
if (args.IsSettingsSelected) if (args.IsSettingsSelected)
{ {
NavigateToPage("Settings"); NavigateToPage("Settings");
@@ -244,20 +250,24 @@ namespace Ink_Canvas.Windows.SettingsViews
try try
{ {
// 页面缓存:已创建的页面直接复用,保留状态,避免重复初始化 _isNavigating = true;
if (!_pages.TryGetValue(pageTag, out var cachedPage)) if (!_pages.TryGetValue(pageTag, out var cachedPage))
{ {
cachedPage = Activator.CreateInstance(pageType); cachedPage = Activator.CreateInstance(pageType);
_pages.Add(pageTag, cachedPage); _pages.Add(pageTag, cachedPage);
} }
// 导航到缓存页面
rootFrame.Navigate(cachedPage.GetType(), cachedPage); rootFrame.Navigate(cachedPage.GetType(), cachedPage);
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show($"导航到页面时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); MessageBox.Show($"导航到页面时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
} }
finally
{
_isNavigating = false;
}
} }
private void OnNavigationViewBackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args) private void OnNavigationViewBackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
@@ -267,7 +277,11 @@ namespace Ink_Canvas.Windows.SettingsViews
private void OnRootFrameNavigated(object sender, NavigationEventArgs e) private void OnRootFrameNavigated(object sender, NavigationEventArgs e)
{ {
// 导航后同步导航项选中状态 if (_isNavigating)
{
return;
}
Type currentPageType = rootFrame.SourcePageType; Type currentPageType = rootFrame.SourcePageType;
// 处理设置项的选中状态 // 处理设置项的选中状态