common/Ui/Common.UI/App.xaml.cs
2026-04-23 17:50:07 +02:00

79 lines
2.3 KiB
C#

namespace Common.UI
{
using Common.UI.Infrastructure;
using System;
using System.Diagnostics;
using System.Windows;
public partial class App : Application
{
internal static void InvokeAsync(Action action)
=> Current.Dispatcher.InvokeAsync(action);
protected override void OnStartup(StartupEventArgs e)
{
if (!this.ShutdownRunningInstance())
{
var mainWindow = new MainWindow();
var args = e.Args; // new object[] { "EregisterFinalize", 1152 }; //
var argsLength = args?.Length ?? 0;
if (argsLength > 0)
{
var source = $"Pages/{args[0]}.xaml";
var sourceUri = new Uri(source, UriKind.RelativeOrAbsolute);
var data = default(object);
if (argsLength > 1)
{
data = args[1];
}
mainWindow.NavigationService.Navigate(sourceUri, data);
}
Current.Exit += (_1, _2) =>
{
VM.ApplicationExit();
};
this.MainWindow = mainWindow;
this.MainWindow.Show();
}
}
private bool ShutdownRunningInstance()
{
var currentProcess = Process.GetCurrentProcess();
var shutdownCalled = false;
foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName))
{
if (process.Id != currentProcess.Id && process.MainModule.FileName == currentProcess.MainModule.FileName)
{
var windowHandle = process.MainWindowHandle;
if (windowHandle != IntPtr.Zero)
{
if (User32DllImports.IsIconic(windowHandle))
{
User32DllImports.ShowWindow(windowHandle, User32Commands.Restore);
}
User32DllImports.SetForegroundWindow(windowHandle);
this.Shutdown();
shutdownCalled = true;
break;
}
}
}
return shutdownCalled;
}
}
}