/ src / modules / EnvironmentVariables / EnvironmentVariables / EnvironmentVariablesXAML / App.xaml.cs
App.xaml.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.IO.Abstractions; 7 8 using EnvironmentVariables.Telemetry; 9 using EnvironmentVariablesUILib; 10 using EnvironmentVariablesUILib.Helpers; 11 using EnvironmentVariablesUILib.Telemetry; 12 using EnvironmentVariablesUILib.ViewModels; 13 using ManagedCommon; 14 using Microsoft.Extensions.DependencyInjection; 15 using Microsoft.Extensions.Hosting; 16 using Microsoft.PowerToys.Telemetry; 17 using Microsoft.UI.Dispatching; 18 using Microsoft.UI.Xaml; 19 20 namespace EnvironmentVariables 21 { 22 /// <summary> 23 /// Provides application-specific behavior to supplement the default Application class. 24 /// </summary> 25 public partial class App : Application 26 { 27 public IHost Host { get; } 28 29 public ETWTrace EtwTrace { get; } = new ETWTrace(); 30 31 public static T GetService<T>() 32 where T : class 33 { 34 if ((App.Current as App)!.Host.Services.GetService(typeof(T)) is not T service) 35 { 36 throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs."); 37 } 38 39 return service; 40 } 41 42 /// <summary> 43 /// Initializes a new instance of the <see cref="App"/> class. 44 /// Initializes the singleton application object. This is the first line of authored code 45 /// executed, and as such is the logical equivalent of main() or WinMain(). 46 /// </summary> 47 public App() 48 { 49 string appLanguage = LanguageHelper.LoadLanguage(); 50 if (!string.IsNullOrEmpty(appLanguage)) 51 { 52 Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage; 53 } 54 55 this.InitializeComponent(); 56 57 Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder().UseContentRoot(AppContext.BaseDirectory).ConfigureServices((context, services) => 58 { 59 services.AddSingleton<IFileSystem, FileSystem>(); 60 services.AddSingleton<IElevationHelper, ElevationHelper>(); 61 services.AddSingleton<IEnvironmentVariablesService, EnvironmentVariablesService>(); 62 services.AddSingleton<ILogger, LoggerWrapper>(); 63 services.AddSingleton<ITelemetry, TelemetryWrapper>(); 64 65 services.AddSingleton<MainViewModel>(); 66 services.AddSingleton<EnvironmentVariablesMainPage>(); 67 }).Build(); 68 69 UnhandledException += App_UnhandledException; 70 } 71 72 private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) 73 { 74 Logger.LogError("Unhandled exception", e.Exception); 75 } 76 77 /// <summary> 78 /// Invoked when the application is launched. 79 /// </summary> 80 /// <param name="args">Details about the launch request and process.</param> 81 protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) 82 { 83 var cmdArgs = Environment.GetCommandLineArgs(); 84 if (cmdArgs?.Length > 1) 85 { 86 if (int.TryParse(cmdArgs[cmdArgs.Length - 1], out int powerToysRunnerPid)) 87 { 88 Logger.LogInfo($"EnvironmentVariables started from the PowerToys Runner. Runner pid={powerToysRunnerPid}."); 89 90 var dispatcher = DispatcherQueue.GetForCurrentThread(); 91 RunnerHelper.WaitForPowerToysRunner(powerToysRunnerPid, () => 92 { 93 Logger.LogInfo("PowerToys Runner exited. Exiting EnvironmentVariables"); 94 dispatcher.TryEnqueue(App.Current.Exit); 95 }); 96 } 97 } 98 else 99 { 100 Logger.LogInfo($"EnvironmentVariables started detached from PowerToys Runner."); 101 } 102 103 PowerToysTelemetry.Log.WriteEvent(new Telemetry.EnvironmentVariablesOpenedEvent()); 104 window = new MainWindow(); 105 window.Activate(); 106 } 107 108 private Window window; 109 } 110 }