From bee7e48cb9c8fe6488997212ab21277ff7a8ddb4 Mon Sep 17 00:00:00 2001 From: Lightczx <1686188646@qq.com> Date: Tue, 19 Dec 2023 11:49:19 +0800 Subject: [PATCH] fix gamePath set null when closing page 2 --- .../Snap.Hutao/Control/ScopedPage.cs | 55 ++++++++++--------- src/Snap.Hutao/Snap.Hutao/MainWindow.xaml.cs | 17 ------ 2 files changed, 28 insertions(+), 44 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao/Control/ScopedPage.cs b/src/Snap.Hutao/Snap.Hutao/Control/ScopedPage.cs index 7fffb423..06db70e8 100644 --- a/src/Snap.Hutao/Snap.Hutao/Control/ScopedPage.cs +++ b/src/Snap.Hutao/Snap.Hutao/Control/ScopedPage.cs @@ -9,10 +9,6 @@ using Snap.Hutao.ViewModel.Abstraction; namespace Snap.Hutao.Control; -/// -/// 表示支持取消加载的异步页面 -/// 在被导航到其他页面前触发取消异步通知 -/// [HighQuality] [SuppressMessage("", "CA1001")] internal class ScopedPage : Page @@ -21,9 +17,8 @@ internal class ScopedPage : Page private readonly CancellationTokenSource viewCancellationTokenSource = new(); private readonly IServiceScope currentScope; - /// - /// 构造一个新的页面 - /// + private bool inFrame = true; + protected ScopedPage() { unloadEventHandler = OnUnloaded; @@ -31,11 +26,6 @@ internal class ScopedPage : Page currentScope = Ioc.Default.GetRequiredService().CreateScope(); } - /// - /// 异步通知接收器 - /// - /// 额外内容 - /// 任务 public async ValueTask NotifyRecipientAsync(INavigationData extra) { if (extra.Data is not null && DataContext is INavigationRecipient recipient) @@ -61,6 +51,32 @@ internal class ScopedPage : Page /// protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) + { + DisposeViewModel(); + inFrame = false; + } + + /// + protected override void OnNavigatedTo(NavigationEventArgs e) + { + if (e.Parameter is INavigationData extra) + { + NotifyRecipientAsync(extra).SafeForget(); + } + } + + private void OnUnloaded(object sender, RoutedEventArgs e) + { + if (inFrame) + { + DisposeViewModel(); + } + + DataContext = null; + Unloaded -= unloadEventHandler; + } + + private void DisposeViewModel() { using (viewCancellationTokenSource) { @@ -79,19 +95,4 @@ internal class ScopedPage : Page } } } - - /// - protected override void OnNavigatedTo(NavigationEventArgs e) - { - if (e.Parameter is INavigationData extra) - { - NotifyRecipientAsync(extra).SafeForget(); - } - } - - private void OnUnloaded(object sender, RoutedEventArgs e) - { - DataContext = null; - Unloaded -= unloadEventHandler; - } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/MainWindow.xaml.cs b/src/Snap.Hutao/Snap.Hutao/MainWindow.xaml.cs index 37d85da2..5b7342b8 100644 --- a/src/Snap.Hutao/Snap.Hutao/MainWindow.xaml.cs +++ b/src/Snap.Hutao/Snap.Hutao/MainWindow.xaml.cs @@ -20,11 +20,8 @@ internal sealed partial class MainWindow : Window, IWindowOptionsSource, IMinMax private const int MinWidth = 1000; private const int MinHeight = 600; - private readonly IServiceProvider serviceProvider; private readonly WindowOptions windowOptions; - private readonly TypedEventHandler closedEventHander; - /// /// 构造一个新的主窗体 /// @@ -32,13 +29,8 @@ internal sealed partial class MainWindow : Window, IWindowOptionsSource, IMinMax public MainWindow(IServiceProvider serviceProvider) { InitializeComponent(); - this.serviceProvider = serviceProvider; windowOptions = new(this, TitleBarView.DragArea, new(1200, 741), true); this.InitializeController(serviceProvider); - - closedEventHander = OnClosed; - - Closed += closedEventHander; } /// @@ -50,13 +42,4 @@ internal sealed partial class MainWindow : Window, IWindowOptionsSource, IMinMax pInfo.ptMinTrackSize.X = (int)Math.Max(MinWidth * scalingFactor, pInfo.ptMinTrackSize.X); pInfo.ptMinTrackSize.Y = (int)Math.Max(MinHeight * scalingFactor, pInfo.ptMinTrackSize.Y); } - - private void OnClosed(object sender, WindowEventArgs args) - { - // The Closed event is raised before XamlRoot is unloaded. - using (serviceProvider.GetRequiredService()) - { - // Thus we can eusure all viewmodels are disposed. - } - } } \ No newline at end of file