1 Star 0 Fork 6

雷一一/Apt

forked from rnchg/Apt 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
App.xaml.cs 4.41 KB
一键复制 编辑 原始数据 按行查看 历史
rnchg 提交于 2024-06-19 19:47 . 添加项目文件。
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;
using General.Apt.App.Views.Pages.App;
using General.Apt.App.Views.Windows;
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 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>>();
Logger.LogInformation("--------Init--------");
}
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();
Logger.LogInformation("--------Startup--------");
}
private async void OnExit(object sender, ExitEventArgs e)
{
Logger.LogInformation("--------Exit--------");
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();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/leeyiyi/apt.git
git@gitee.com:leeyiyi/apt.git
leeyiyi
apt
Apt
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385