fix gamePath set null when closing page 2

This commit is contained in:
Lightczx
2023-12-19 11:49:19 +08:00
parent 83cbc9bbe1
commit bee7e48cb9
2 changed files with 28 additions and 44 deletions

View File

@@ -9,10 +9,6 @@ using Snap.Hutao.ViewModel.Abstraction;
namespace Snap.Hutao.Control;
/// <summary>
/// 表示支持取消加载的异步页面
/// 在被导航到其他页面前触发取消异步通知
/// </summary>
[HighQuality]
[SuppressMessage("", "CA1001")]
internal class ScopedPage : Page
@@ -21,9 +17,8 @@ internal class ScopedPage : Page
private readonly CancellationTokenSource viewCancellationTokenSource = new();
private readonly IServiceScope currentScope;
/// <summary>
/// 构造一个新的页面
/// </summary>
private bool inFrame = true;
protected ScopedPage()
{
unloadEventHandler = OnUnloaded;
@@ -31,11 +26,6 @@ internal class ScopedPage : Page
currentScope = Ioc.Default.GetRequiredService<IScopedPageScopeReferenceTracker>().CreateScope();
}
/// <summary>
/// 异步通知接收器
/// </summary>
/// <param name="extra">额外内容</param>
/// <returns>任务</returns>
public async ValueTask NotifyRecipientAsync(INavigationData extra)
{
if (extra.Data is not null && DataContext is INavigationRecipient recipient)
@@ -61,6 +51,32 @@ internal class ScopedPage : Page
/// <inheritdoc/>
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
DisposeViewModel();
inFrame = false;
}
/// <inheritdoc/>
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
}
}
}
/// <inheritdoc/>
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;
}
}

View File

@@ -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<object, WindowEventArgs> closedEventHander;
/// <summary>
/// 构造一个新的主窗体
/// </summary>
@@ -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;
}
/// <inheritdoc/>
@@ -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<IScopedPageScopeReferenceTracker>())
{
// Thus we can eusure all viewmodels are disposed.
}
}
}