livia-test/Astroke/App.xaml.cs
2025-03-28 14:31:53 +08:00

65 lines
2.3 KiB
C#

using System.Windows;
using Astroke.ViewModels;
using Astroke.Views.Controls;
using JetBrains.Annotations;
using Livia.Models;
using Livia.Models.Data;
using Livia.Properties;
using Livia.Utility.DependencyInjection;
using Livia.ViewModels;
using Livia.Views;
using Livia.Views.Utility;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Velopack;
namespace Astroke;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
private readonly ILogger _logger;
private readonly IServiceProvider _serviceProvider;
private readonly IWarningSystem _warningSystem;
[UsedImplicitly] private Mutex? _mutex;
public App()
{
ServiceConfigurations.AppName = "astroke";
VelopackApp.Build().Run();
ServiceProviderFactory.Init(sc => sc.AddSingleton<ILiviaControlViewModel, AStrokeControlViewModel>());
InitializeComponent();
_serviceProvider = ServiceProviderFactory.ServiceProvider;
_logger = ActivatorUtilities.GetServiceOrCreateInstance<ILogger<App>>(ServiceProviderFactory.ServiceProvider);
_warningSystem = ActivatorUtilities.GetServiceOrCreateInstance<IWarningSystem>(ServiceProviderFactory.ServiceProvider);
_logger.LogInformation("App Starting");
SettingsLogger.Init();
AppHelper helper = ActivatorUtilities.GetServiceOrCreateInstance<AppHelper>(ServiceProviderFactory.ServiceProvider);
Dispatcher.UnhandledException += helper.OnDispatcherUnhandledException;
helper.UpdateSettings();
helper.LoadStrings();
}
private void OnStartup(object sender, StartupEventArgs eventArgs)
{
_mutex = new Mutex(true, "Livia", out bool isNewInstance);
if (!isNewInstance)
{
_warningSystem.ShowDialog(WarningWindowKind.Warning, true, "InstanceIsRunningError");
Current.Shutdown();
}
MainWindow mainWindow = ActivatorUtilities.CreateInstance<MainWindow>(_serviceProvider);
mainWindow.AddMainControl(new AStrokeControl(ActivatorUtilities.GetServiceOrCreateInstance<ILiviaControlViewModel>(ServiceProviderFactory.ServiceProvider)));
MainWindow = mainWindow;
mainWindow.Show();
_logger.LogInformation("App Started");
}
}