fix Launch Game ViewModel scope

This commit is contained in:
Lightczx
2024-06-05 13:42:31 +08:00
parent db15b6a30c
commit 0dcba220c5
5 changed files with 29 additions and 12 deletions

View File

@@ -5,6 +5,7 @@ using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Markup;
using Microsoft.UI.Xaml.Navigation;
using Snap.Hutao.Core.Abstraction;
using Snap.Hutao.Service.Navigation;
using Snap.Hutao.View.Helper;
using Snap.Hutao.ViewModel.Abstraction;
@@ -53,9 +54,14 @@ internal class ScopedPage : Page
{
try
{
IViewModel viewModel = pageScope.ServiceProvider.GetRequiredService<TViewModel>();
viewModel.CancellationToken = viewCancellationTokenSource.Token;
viewModel.DeferContentLoader = new DeferContentLoader(this);
TViewModel viewModel = pageScope.ServiceProvider.GetRequiredService<TViewModel>();
using (viewModel.DisposeLock.Enter())
{
viewModel.IsViewDisposed = false;
viewModel.CancellationToken = viewCancellationTokenSource.Token;
viewModel.DeferContentLoader = new DeferContentLoader(this);
}
DataContext = viewModel;
}
catch (Exception ex)
@@ -104,13 +110,15 @@ internal class ScopedPage : Page
viewCancellationTokenSource.Cancel();
IViewModel viewModel = (IViewModel)DataContext;
// Wait to ensure viewmodel operation is completed
viewModel.DisposeLock.Wait();
viewModel.IsViewDisposed = true;
using (viewModel.DisposeLock.Enter())
{
// Wait to ensure viewmodel operation is completed
viewModel.IsViewDisposed = true;
// Dispose the scope
pageScope.Dispose();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true);
// Dispose the scope
pageScope.Dispose();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true);
}
}
}
}

View File

@@ -5,5 +5,5 @@ namespace Snap.Hutao.Core.Abstraction;
internal interface IPinnable<TData>
{
ref readonly TData GetPinnableReference();
ref TData GetPinnableReference();
}

View File

@@ -0,0 +1,9 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Core.Abstraction;
internal interface IResurrectable
{
void Resurrect();
}

View File

@@ -2,7 +2,7 @@
"profiles": {
"Snap.Hutao": {
"commandName": "MsixPackage",
"nativeDebugging": false,
"nativeDebugging": true,
"doNotLaunchApp": false,
"allowLocalNetworkLoopbackProperty": true
},

View File

@@ -100,6 +100,6 @@ internal sealed partial class LaunchGameShared
[Command("HandleGamePathNullOrEmptyCommand")]
private void HandleGamePathNullOrEmpty()
{
navigationService.Navigate<LaunchGamePage>(INavigationAwaiter.Default);
navigationService.Navigate<LaunchGamePage>(INavigationAwaiter.Default, true);
}
}