-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.axaml.cs
More file actions
73 lines (63 loc) · 1.98 KB
/
App.axaml.cs
File metadata and controls
73 lines (63 loc) · 1.98 KB
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
/// <summary>
/// Main application class
/// </summary>
using System;
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using ChemLocalLink.Services;
using ChemLocalLink.Utilities;
using ChemLocalLink.ViewModels;
using ChemLocalLink.Views;
using Microsoft.Extensions.DependencyInjection;
namespace ChemLocalLink;
public class App : Application
{
private IServiceProvider? _serviceProvider;
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
// configure DI with the notification manager
var services = new ServiceCollection();
services.AddApplicationServices(Program.NotificationManager);
_serviceProvider = services.BuildServiceProvider();
var mw = new MainWindowView();
var viewModel = _serviceProvider.GetRequiredService<MainWindowViewModel>();
var windowService = _serviceProvider.GetRequiredService<IWindowService>();
viewModel.Initialize(mw, desktop.Args ?? []);
mw.DataContext = viewModel;
desktop.Startup += DesktopOnStartup;
desktop.MainWindow = mw;
desktop.MainWindow.DataContext = mw.DataContext;
}
base.OnFrameworkInitializationCompleted();
}
private void DesktopOnStartup(object? sender, ControlledApplicationLifetimeStartupEventArgs e)
{
var currentProcess = Process.GetCurrentProcess();
if (Process.GetProcessesByName("ChemLocalLink").Length > 0)
{
var processes = Process.GetProcessesByName("ChemLocalLink");
foreach (Process process in processes)
{
if (process.Id != currentProcess.Id)
{
try
{
process.Kill();
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to kill process {process.Id}: {ex.Message}");
}
}
}
}
}
}