更新整理
This commit is contained in:
71
WPFluent.Gallery/Services/ApplicationHostService.cs
Normal file
71
WPFluent.Gallery/Services/ApplicationHostService.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
// 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;
|
||||
|
||||
/// <summary>
|
||||
/// Managed host of the application.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <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 Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return 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 Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates main window during activation.
|
||||
/// </summary>
|
||||
private Task HandleActivationAsync()
|
||||
{
|
||||
if (Application.Current.Windows.OfType<MainWindow>().Any())
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
IWindow mainWindow = _serviceProvider.GetRequiredService<IWindow>();
|
||||
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));
|
||||
}
|
||||
}
|
||||
13
WPFluent.Gallery/Services/Contracts/IWindow.cs
Normal file
13
WPFluent.Gallery/Services/Contracts/IWindow.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
// 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.
|
||||
|
||||
namespace WPFluent.Gallery.Services.Contracts;
|
||||
|
||||
public interface IWindow
|
||||
{
|
||||
event RoutedEventHandler Loaded;
|
||||
|
||||
void Show();
|
||||
}
|
||||
31
WPFluent.Gallery/Services/WindowsProviderService.cs
Normal file
31
WPFluent.Gallery/Services/WindowsProviderService.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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.
|
||||
|
||||
namespace WPFluent.Gallery.Services;
|
||||
|
||||
public class WindowsProviderService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public WindowsProviderService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public void Show<T>()
|
||||
where T : class
|
||||
{
|
||||
if (!typeof(Window).IsAssignableFrom(typeof(T)))
|
||||
{
|
||||
throw new InvalidOperationException($"The window class should be derived from {typeof(Window)}.");
|
||||
}
|
||||
|
||||
Window windowInstance =
|
||||
_serviceProvider.GetService<T>() as Window
|
||||
?? throw new InvalidOperationException("Window is not registered as service.");
|
||||
windowInstance.Owner = Application.Current.MainWindow;
|
||||
windowInstance.Show();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user