| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using Microsoft.Extensions.DependencyInjection; |
| 4 | using Microsoft.Extensions.Hosting; |
| 5 | using Microsoft.UI.Xaml; |
| 6 | using WslSettings.Activation; |
| 7 | using WslSettings.Contracts.Services; |
| 8 | using WslSettings.Services; |
| 9 | using WslSettings.ViewModels; |
| 10 | using WslSettings.ViewModels.OOBE; |
| 11 | using WslSettings.ViewModels.Settings; |
| 12 | using WslSettings.Views.OOBE; |
| 13 | using WslSettings.Views.Settings; |
| 14 | |
| 15 | namespace WslSettings; |
| 16 | |
| 17 | // To learn more about WinUI 3, see https://docs.microsoft.com/windows/apps/winui/winui3/. |
| 18 | public partial class App : Application |
| 19 | { |
| 20 | // The .NET Generic Host provides dependency injection, configuration, logging, and other services. |
| 21 | // https://docs.microsoft.com/dotnet/core/extensions/generic-host |
| 22 | // https://docs.microsoft.com/dotnet/core/extensions/dependency-injection |
| 23 | // https://docs.microsoft.com/dotnet/core/extensions/configuration |
| 24 | // https://docs.microsoft.com/dotnet/core/extensions/logging |
| 25 | public IHost Host |
| 26 | { |
| 27 | get; |
| 28 | } |
| 29 | |
| 30 | public static T GetService<T>() |
| 31 | where T : class |
| 32 | { |
| 33 | if ((App.Current as App)!.Host.Services.GetService(typeof(T)) is not T service) |
| 34 | { |
| 35 | throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs."); |
| 36 | } |
| 37 | |
| 38 | return service; |
| 39 | } |
| 40 | |
| 41 | public static MainWindow? MainWindow { get; set; } |
| 42 | |
| 43 | public static OOBEWindow? OOBEWindow { get; set; } |
| 44 | |
| 45 | public static UIElement? AppTitlebar { get; set; } |
| 46 | |
| 47 | public App() |
| 48 | { |
| 49 | InitializeComponent(); |
| 50 | |
| 51 | Host = Microsoft.Extensions.Hosting.Host. |
| 52 | CreateDefaultBuilder(). |
| 53 | UseContentRoot(AppContext.BaseDirectory). |
| 54 | ConfigureServices((context, services) => |
| 55 | { |
| 56 | // Default Activation Handler |
| 57 | services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>(); |
| 58 | |
| 59 | // Other Activation Handlers |
| 60 | services.AddTransient<IActivationHandler, ProtocolActivationHandler>(); |
| 61 | |
| 62 | // Services |
| 63 | services.AddTransient<INavigationViewService, NavigationViewService>(); |
| 64 | |
| 65 | services.AddSingleton<IActivationService, ActivationService>(); |
| 66 | services.AddSingleton<IPageService, PageService>(); |
| 67 | services.AddSingleton<INavigationService, NavigationService>(); |
| 68 | services.AddSingleton<IWindowService, WindowService>(); |
| 69 | services.AddSingleton<IWslConfigService, WslConfigService>(); |
| 70 | |
| 71 | // Views and ViewModels |
| 72 | services.AddTransient<AboutViewModel>(); |
| 73 | services.AddTransient<AboutPage>(); |
| 74 | services.AddTransient<DeveloperViewModel>(); |
| 75 | services.AddTransient<DeveloperPage>(); |
| 76 | services.AddTransient<FileSystemViewModel>(); |
| 77 | services.AddTransient<FileSystemPage>(); |
| 78 | services.AddTransient<MemAndProcViewModel>(); |
| 79 | services.AddTransient<MemAndProcPage>(); |
| 80 | services.AddTransient<NetworkingViewModel>(); |
| 81 | services.AddTransient<NetworkingPage>(); |
| 82 | services.AddTransient<OptionalFeaturesViewModel>(); |
| 83 | services.AddTransient<OptionalFeaturesPage>(); |
| 84 | services.AddTransient<ShellViewModel>(); |
| 85 | services.AddTransient<Views.Settings.ShellPage>(); |
| 86 | |
| 87 | services.AddTransient<DistroManagementViewModel>(); |
| 88 | services.AddTransient<DistroManagementPage>(); |
| 89 | services.AddTransient<DockerDesktopIntegrationViewModel>(); |
| 90 | services.AddTransient<DockerDesktopIntegrationPage>(); |
| 91 | services.AddTransient<GeneralViewModel>(); |
| 92 | services.AddTransient<GeneralPage>(); |
| 93 | services.AddTransient<GPUAccelerationViewModel>(); |
| 94 | services.AddTransient<GPUAccelerationPage>(); |
| 95 | services.AddTransient<GUIAppsViewModel>(); |
| 96 | services.AddTransient<GUIAppsPage>(); |
| 97 | services.AddTransient<NetworkingIntegrationViewModel>(); |
| 98 | services.AddTransient<NetworkingIntegrationPage>(); |
| 99 | services.AddTransient<VSCodeIntegrationViewModel>(); |
| 100 | services.AddTransient<VSCodeIntegrationPage>(); |
| 101 | services.AddTransient<VSIntegrationViewModel>(); |
| 102 | services.AddTransient<VSIntegrationPage>(); |
| 103 | services.AddTransient<WorkingAcrossFileSystemsViewModel>(); |
| 104 | services.AddTransient<WorkingAcrossFileSystemsPage>(); |
| 105 | services.AddTransient<Views.OOBE.ShellPage>(); |
| 106 | |
| 107 | // Configuration |
| 108 | }). |
| 109 | Build(); |
| 110 | |
| 111 | UnhandledException += App_UnhandledException; |
| 112 | } |
| 113 | |
| 114 | private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) |
| 115 | { |
| 116 | // TODO: Log and handle exceptions as appropriate. |
| 117 | // https://docs.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.unhandledexception. |
| 118 | } |
| 119 | |
| 120 | protected async override void OnLaunched(LaunchActivatedEventArgs args) |
| 121 | { |
| 122 | base.OnLaunched(args); |
| 123 | |
| 124 | await App.GetService<IActivationService>().ActivateAsync(args); |
| 125 | } |
| 126 | } |