Files
WindowsMusicPlayer-TheUntam…/The Untamed Music Player/App.xaml.cs
2024-08-27 15:07:06 +08:00

126 lines
4.7 KiB
C#

using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using The_Untamed_Music_Player.Activation;
using The_Untamed_Music_Player.Contracts.Services;
using The_Untamed_Music_Player.Core.Contracts.Services;
using The_Untamed_Music_Player.Core.Services;
using The_Untamed_Music_Player.Models;
using The_Untamed_Music_Player.Services;
using The_Untamed_Music_Player.ViewModels;
using The_Untamed_Music_Player.Views;
namespace The_Untamed_Music_Player;
// To learn more about WinUI 3, see https://docs.microsoft.com/windows/apps/winui/winui3/.
public partial class App : Application
{
// The .NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
// https://docs.microsoft.com/dotnet/core/extensions/configuration
// https://docs.microsoft.com/dotnet/core/extensions/logging
public IHost Host
{
get;
}
public static T GetService<T>()
where T : class
{
if ((App.Current as App)!.Host.Services.GetService(typeof(T)) is not T service)
{
throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs.");
}
return service;
}
public static WindowEx? MainWindow
{
get; private set;
}
public static UIElement? AppTitlebar
{
get; set;
}
public App()
{
Debug.WriteLine("App.xaml.cs: Constructor");
InitializeComponent();
Host = Microsoft.Extensions.Hosting.Host.
CreateDefaultBuilder().
UseContentRoot(AppContext.BaseDirectory).
ConfigureServices((context, services) =>//注册服务信息
{
// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();
// Other Activation Handlers
// Services
services.AddSingleton<ILocalSettingsService, LocalSettingsService>();
services.AddSingleton<IThemeSelectorService, ThemeSelectorService>();
services.AddTransient<INavigationViewService, NavigationViewService>();
services.AddSingleton<IActivationService, ActivationService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
// Core Services
services.AddSingleton<IFileService, FileService>();
// Views and ViewModels
services.AddTransient<SettingsViewModel>();
services.AddTransient<SettingsPage>();
services.AddTransient<ViewModel>();
services.AddTransient<Page>();
services.AddTransient<ViewModel>();
services.AddTransient<Page>();
services.AddTransient<ViewModel>();
services.AddTransient<Page>();
services.AddTransient<ViewModel>();
services.AddTransient<Page>();
services.AddTransient<ShellPage>();
services.AddTransient<ShellViewModel>();
services.AddTransient<RootPlayBarView>();
services.AddTransient<RootPlayBarViewModel>();
services.AddTransient<Page>();
services.AddTransient<ViewModel>();
services.AddTransient<Page>();
services.AddTransient<ViewModel>();
services.AddTransient<Page>();
services.AddTransient<ViewModel>();
services.AddTransient<Page>();
services.AddTransient<ViewModel>();
services.AddTransient<Page>();
services.AddTransient<ViewModel>();
services.AddTransient<Page>();
services.AddTransient<ViewModel>();
// Configuration
services.Configure<LocalSettingsOptions>(context.Configuration.GetSection(nameof(LocalSettingsOptions)));
}).
Build();//生成容器
Debug.WriteLine($"AppMainWindow is null: {MainWindow == null}");
Debug.WriteLine("App.xaml.cs: Constructor End");
}
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);
MainWindow = new MainWindow(GetService<ILocalSettingsService>());
Debug.WriteLine($"OnMainWindow is null: {MainWindow == null}");
await GetService<IActivationService>().ActivateAsync(args);
}
}