Skip to content

Instantly share code, notes, and snippets.

@VergilGao
Created August 29, 2024 05:22
Show Gist options
  • Save VergilGao/9d520af96214e7c5742304bc9253afaf to your computer and use it in GitHub Desktop.
Save VergilGao/9d520af96214e7c5742304bc9253afaf to your computer and use it in GitHub Desktop.
WPF 通用主机
private IHost host = null!;
public static IServiceProvider ServiceProvider { get; private set; } = null!;
protected override async void OnStartup(StartupEventArgs e)
{
host = CreateHostBuilder(e.Args).Build();
ServiceProvider = host.Services;
await host.StartAsync();
MainWindow = host.Services.GetRequiredService<MainWindow>();
MainWindow.Show();
base.OnStartup(e);
}
protected override async void OnExit(ExitEventArgs e)
{
await host.StopAsync();
host.Dispose();
base.OnExit(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment