// This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. // Copyright (C) Leszek Pomianowski and WPF UI Contributors. // All Rights Reserved. using Microsoft.Extensions.Hosting; using WPFluent.Gallery.Services.Contracts; using WPFluent.Gallery.Views.Pages; using WPFluent.Gallery.Views.Windows; namespace WPFluent.Gallery.Services; /// /// Managed host of the application. /// public class ApplicationHostService : IHostedService { private readonly IServiceProvider _serviceProvider; public ApplicationHostService(IServiceProvider serviceProvider) { // If you want, you can do something with these services at the beginning of loading the application. _serviceProvider = serviceProvider; } /// /// Triggered when the application host is ready to start the service. /// /// Indicates that the start process has been aborted. public Task StartAsync(CancellationToken cancellationToken) { return HandleActivationAsync(); } /// /// Triggered when the application host is performing a graceful shutdown. /// /// Indicates that the shutdown process should no longer be graceful. public Task StopAsync(CancellationToken cancellationToken) { return Task.CompletedTask; } /// /// Creates main window during activation. /// private Task HandleActivationAsync() { if (Application.Current.Windows.OfType().Any()) { return Task.CompletedTask; } IWindow mainWindow = _serviceProvider.GetRequiredService(); mainWindow.Loaded += OnMainWindowLoaded; mainWindow?.Show(); return Task.CompletedTask; } private void OnMainWindowLoaded(object sender, RoutedEventArgs e) { if (sender is not MainWindow mainWindow) { return; } _ = mainWindow.NavigationView.Navigate(typeof(DashboardPage)); } }