整理控件库
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
@@ -7,24 +7,15 @@ namespace WPFluent.Gallery.Services;
|
||||
|
||||
public class WindowsProviderService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public WindowsProviderService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public void Show<T>()
|
||||
where T : class
|
||||
where T : Window, new()
|
||||
{
|
||||
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.");
|
||||
Window windowInstance = new T();
|
||||
windowInstance.Owner = Application.Current.MainWindow;
|
||||
windowInstance.Show();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user