-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApp.xaml.cs
51 lines (44 loc) · 1.45 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Windows;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace wpfGenericHost;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public sealed partial class App
{
private readonly IHost _host;
public App()
{
_host = new HostBuilder()
.ConfigureAppConfiguration((context, configurationBuilder) =>
{
configurationBuilder.SetBasePath(context.HostingEnvironment.ContentRootPath);
configurationBuilder.AddJsonFile("appsettings.json", optional: false);
})
.ConfigureServices((context, services) =>
{
services.Configure<Settings>(context.Configuration);
services.AddSingleton<ITextService, TextService>();
services.AddSingleton<MainWindow>();
})
.ConfigureLogging(logging => { logging.AddConsole(); })
.Build();
}
private async void Application_Startup(object sender, StartupEventArgs e)
{
await _host.StartAsync();
var mainWindow = _host.Services.GetService<MainWindow>();
mainWindow.Show();
}
private async void Application_Exit(object sender, ExitEventArgs e)
{
using (_host)
{
await _host.StopAsync(TimeSpan.FromSeconds(5));
}
}
}