forked from rnchg/Apt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
98 lines (90 loc) · 4.34 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using General.Apt.App.Models;
using General.Apt.App.Services;
using General.Apt.App.Services.Contracts;
using General.Apt.App.Utility;
using General.Apt.App.ViewModels.Pages.App;
using General.Apt.App.ViewModels.Windows.App;
using General.Apt.App.Views.Pages.App;
using General.Apt.App.Views.Windows.App;
using General.Apt.Service.Extensions;
using Microsoft.Extensions.Logging;
using Serilog;
using Wpf.Ui;
namespace General.Apt.App
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public new static App Current => (App)Application.Current;
public IHost Host { get; }
public Microsoft.Extensions.Logging.ILogger Logger { get; }
public T GetService<T>() where T : class => Host.Services.GetService<T>();
public T GetRequiredService<T>() where T : class => Host.Services.GetRequiredService<T>();
public static AssemblyName EntryAssembly { get; } = Assembly.GetEntryAssembly().GetName();
public App()
{
var builder = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder();
var appSettings = builder.Configuration.Get<AppSettings>();
builder.Services.AddSingleton(appSettings);
builder.Services.AddSerilog((services, logger) => logger.ReadFrom.Configuration(builder.Configuration));
builder.Services.AddSingleton<IWindow, MainWindow>();
builder.Services.AddSingleton<MainWindowViewModel>();
builder.Services.AddSingleton<INavigationService, NavigationService>();
builder.Services.AddSingleton<ISnackbarService, SnackbarService>();
builder.Services.AddSingleton<IContentDialogService, ContentDialogService>();
builder.Services.AddSingleton<WindowsProviderService>();
builder.Services.AddSingleton<DashboardPage>();
builder.Services.AddSingleton<DashboardPageViewModel>();
builder.Services.AddSingleton<SettingsPage>();
builder.Services.AddSingleton<SettingsPageViewModel>();
builder.Services.AddFromNamespace(ServiceLifetime.Singleton, "General.Apt.App.Views", Assembly.GetExecutingAssembly());
builder.Services.AddFromNamespace(ServiceLifetime.Singleton, "General.Apt.App.ViewModels", Assembly.GetExecutingAssembly());
builder.Services.AddHostedService<AppHostService>();
Host = builder.Build();
Logger = GetRequiredService<ILogger<App>>();
}
private async void OnStartup(object sender, StartupEventArgs e)
{
var processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
var processes = System.Diagnostics.Process.GetProcessesByName(processName);
if (processes.Length > 1)
{
Dialog.ShowErrorDialog("The program is running and cannot be restarted!");
Environment.Exit(1);
}
Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
await Host.StartAsync();
}
private async void OnExit(object sender, ExitEventArgs e)
{
await Host.StopAsync();
Host.Dispose();
Environment.Exit(0);
}
private void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
var exception = e.Exception;
ExceptionHandler(exception.GetBaseException());
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = e.ExceptionObject as Exception;
ExceptionHandler(exception.GetBaseException());
}
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
var exception = e.Exception;
ExceptionHandler(exception.GetBaseException());
}
private void ExceptionHandler(Exception exception)
{
Logger.LogError(exception.ToString());
Dialog.ShowErrorDialog(exception.Message);
Host.StopAsync().GetAwaiter().GetResult();
}
}
}