-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
74 lines (62 loc) · 2.02 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using WPFTodo.HostBuilder;
using WPFTodo.Models;
using WPFTodo.Services.DataProvider;
using WPFTodo.Services.NavigationService;
using WPFTodo.Services.Provider;
using WPFTodo.Stores;
using WPFTodo.ViewModels;
namespace WPFTodo;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private readonly IHost _host;
public App()
{
_host = Host.CreateDefaultBuilder()
.AddViewModels()
.ConfigureServices((context, services) =>
{
IPersistentDataManager persistentDataManager = new JsonPersistentDataManager();
services.AddSingleton(persistentDataManager);
services.AddSingleton((s) => new AppStore(persistentDataManager));
services.AddSingleton<NavigationBackService>();
services.AddSingleton<NavigationStore>();
services.AddSingleton<MainViewModel>();
services.AddSingleton(s => new MainWindow()
{
DataContext = s.GetRequiredService<MainViewModel>()
});
})
.Build();
}
protected override void OnStartup(StartupEventArgs e)
{
_host.Start();
MainWindow = _host.Services.GetRequiredService<MainWindow>();
MainWindow.Show();
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
SavePersistentData();
_host.Dispose();
base.OnExit(e);
}
private void SavePersistentData()
{
var appStore = _host.Services.GetRequiredService<AppStore>();
var persistentDataManager = _host.Services.GetRequiredService<IPersistentDataManager>();
persistentDataManager.SaveData(appStore.PersistentData);
}
}