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 { /// /// App.xaml 的交互逻辑 /// 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(); // Main window container with navigation _ = services.AddSingleton(); _ = services.AddSingleton(); _ = services.AddSingleton(); _ = services.AddSingleton(); _ = services.AddSingleton(); _ = services.AddSingleton(); // Top-level pages _ = services.AddSingleton(); _ = services.AddSingleton(); //_ = services.AddSingleton(); _ = services.AddSingleton(); _ = services.AddSingleton(); _ = services.AddSingleton(); // 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(new("pl-PL")); //}); } ) .Build(); /// /// /// /// /// /// /// public static IServiceCollection AddTransientFromNamespace(IServiceCollection services, string namespaceName, params Assembly[] assemblies) { foreach (Assembly assembly in assemblies) { IEnumerable 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; } /// /// Gets registered service. /// /// Type of the service to get. /// Instance of the service or . public static T GetRequiredService() where T : class { return _host.Services.GetRequiredService(); } /// /// Occurs when the application is loading. /// private void OnStartup(object sender, StartupEventArgs e) { _host.Start(); } /// /// Occurs when the application is closing. /// private void OnExit(object sender, ExitEventArgs e) { _host.StopAsync().Wait(); _host.Dispose(); } /// /// Occurs when an exception is thrown by an application but not handled. /// 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 } } }