Files
better-genshin-impact/BetterGenshinImpact/Service/ApplicationHostService.cs
辉鸭蛋 46ebdfc452 wpfui 4.0.0 (#1255)
* new INavigationAware, remove PageService

* fix run

* fix title bar

* override OnNavigatedTo

* try fix WindowStartupLocation
2025-03-13 00:33:31 +08:00

55 lines
1.6 KiB
C#

using BetterGenshinImpact.View;
using BetterGenshinImpact.View.Pages;
using Microsoft.Extensions.Hosting;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Wpf.Ui;
namespace BetterGenshinImpact.Service;
/// <summary>
/// Managed host of the application.
/// </summary>
public class ApplicationHostService(IServiceProvider serviceProvider) : IHostedService
{
private INavigationWindow? _navigationWindow;
/// <summary>
/// Triggered when the application host is ready to start the service.
/// </summary>
/// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
public async Task StartAsync(CancellationToken cancellationToken)
{
await HandleActivationAsync();
}
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// </summary>
/// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
public async Task StopAsync(CancellationToken cancellationToken)
{
await Task.CompletedTask;
}
/// <summary>
/// Creates main window during activation.
/// </summary>
private async Task HandleActivationAsync()
{
await Task.CompletedTask;
if (!Application.Current.Windows.OfType<MainWindow>().Any())
{
_navigationWindow = (serviceProvider.GetService(typeof(INavigationWindow)) as INavigationWindow)!;
_navigationWindow!.ShowWindow();
_ = _navigationWindow.Navigate(typeof(HomePage));
}
await Task.CompletedTask;
}
}