Files
ShrlAlgoToolkit/WPFluent.Gallery/App.xaml.cs
2025-04-24 20:56:44 +08:00

149 lines
5.1 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System.Reflection;
using WPFluent.Gallery.Services;
using WPFluent.Gallery.Services.Contracts;
using WPFluent.Gallery.ViewModels;
using WPFluent.Gallery.ViewModels.Pages;
using WPFluent.Gallery.ViewModels.Windows;
using WPFluent.Gallery.Views.Pages;
using WPFluent.Gallery.Views.Windows;
namespace WPFluent.Gallery
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
public App()
{
//SystemMenuThemeManager.Apply();
//Splash.ShowAsync("pack://application:,,,/WPFluent.Gallery;component/wpfui.png", 0.98d);
InitializeComponent();
}
private static readonly IHost _host = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(c =>
{
_ = c.SetBasePath(AppContext.BaseDirectory);
})
.ConfigureServices(
(_1, services) =>
{
_ = services.AddNavigationViewPageProvider();
// App Host
_ = services.AddHostedService<ApplicationHostService>();
// Main window container with navigation
_ = services.AddSingleton<IWindow, MainWindow>();
_ = services.AddSingleton<MainWindowViewModel>();
_ = services.AddSingleton<INavigationService, NavigationService>();
_ = services.AddSingleton<ISnackbarService, SnackbarService>();
_ = services.AddSingleton<IContentDialogService, ContentDialogService>();
_ = services.AddSingleton<WindowsProviderService>();
// Top-level pages
_ = services.AddSingleton<DashboardPage>();
_ = services.AddSingleton<DashboardViewModel>();
//_ = services.AddSingleton<AllControlsPage>();
_ = services.AddSingleton<AllControlsViewModel>();
_ = services.AddSingleton<SettingsPage>();
_ = services.AddSingleton<SettingsViewModel>();
// All other pages and view models
_ = AddTransientFromNamespace(services, "WPFluent.Gallery.Views", Assembly.GetExecutingAssembly());
_ = AddTransientFromNamespace(services,
"WPFluent.Gallery.ViewModels",
Assembly.GetExecutingAssembly()
);
//_ = services.AddStringLocalizer(b =>
//{
// b.FromResource<Translations>(new("pl-PL"));
//});
}
)
.Build();
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <param name="namespaceName"></param>
/// <param name="assemblies"></param>
/// <returns></returns>
public static IServiceCollection AddTransientFromNamespace(IServiceCollection services, string namespaceName, params Assembly[] assemblies)
{
foreach (Assembly assembly in assemblies)
{
IEnumerable<Type> types = assembly
.GetTypes()
.Where(x =>
x.IsClass
&& x.Namespace!.StartsWith(namespaceName, StringComparison.InvariantCultureIgnoreCase)
);
foreach (Type? type in types)
{
if (services.All(x => x.ServiceType != type))
{
if (type == typeof(ViewModel))
{
continue;
}
_ = services.AddTransient(type);
}
}
}
return services;
}
/// <summary>
/// Gets registered service.
/// </summary>
/// <typeparam name="T">Type of the service to get.</typeparam>
/// <returns>Instance of the service or <see langword="null"/>.</returns>
public static T GetRequiredService<T>()
where T : class
{
return _host.Services.GetRequiredService<T>();
}
/// <summary>
/// Occurs when the application is loading.
/// </summary>
private void OnStartup(object sender, StartupEventArgs e)
{
_host.Start();
}
/// <summary>
/// Occurs when the application is closing.
/// </summary>
private void OnExit(object sender, ExitEventArgs e)
{
_host.StopAsync().Wait();
_host.Dispose();
}
/// <summary>
/// Occurs when an exception is thrown by an application but not handled.
/// </summary>
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// For more info see https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-6.0
}
}
}