WIn_RustTools/RustTools/App.xaml.cs

138 lines
4.8 KiB
C#
Raw Normal View History

2024-07-19 07:58:15 +00:00
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
2024-07-14 11:24:10 +00:00
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
using RustTools.Activation;
using RustTools.Contracts.Services;
using RustTools.Core.Contracts.Services;
using RustTools.Core.Services;
using RustTools.Helpers;
using RustTools.Models;
using RustTools.Services;
using RustTools.ViewModels;
using RustTools.Views;
2024-07-19 07:58:15 +00:00
using System.Threading;
using RustTools.muqing;
2024-07-14 11:24:10 +00:00
namespace RustTools;
// To learn more about WinUI 3, see https://docs.microsoft.com/windows/apps/winui/winui3/.
public partial class App : Application
{
// The .NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
// https://docs.microsoft.com/dotnet/core/extensions/configuration
// https://docs.microsoft.com/dotnet/core/extensions/logging
public IHost Host
{
get;
}
public static T GetService<T>()
where T : class
{
if ((App.Current as App)!.Host.Services.GetService(typeof(T)) is not T service)
{
throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs.");
}
return service;
}
public static WindowEx MainWindow { get; } = new MainWindow();
public static UIElement? AppTitlebar { get; set; }
public App()
{
InitializeComponent();
2024-07-19 07:58:15 +00:00
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("RustTools");//获取指定的进程名
if (myProcesses.Length > 1) //如果可以获取到知道的进程名则说明已经启动
{
Exit(); //关闭系统
}
2024-07-14 11:24:10 +00:00
Host = Microsoft.Extensions.Hosting.Host.
CreateDefaultBuilder().
UseContentRoot(AppContext.BaseDirectory).
ConfigureServices((context, services) =>
{
// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();
// Other Activation Handlers
// Services
services.AddSingleton<ILocalSettingsService, LocalSettingsService>();
services.AddSingleton<IThemeSelectorService, ThemeSelectorService>();
services.AddTransient<INavigationViewService, NavigationViewService>();
services.AddSingleton<IActivationService, ActivationService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
// Core Services
services.AddSingleton<IFileService, FileService>();
// Views and ViewModels
services.AddTransient<SettingsViewModel>();
services.AddTransient<SettingsPage>();
services.AddTransient<HomePageViewModel>();
services.AddTransient<HomePage>();
2024-07-15 05:22:53 +00:00
services.AddTransient<ConcernViewModel>();
services.AddTransient<Concern>();
services.AddTransient<RankingViewModel>();
services.AddTransient<RankingPage>();
services.AddTransient<UserViewModel>();
services.AddTransient<UserPage>();
2024-07-14 11:24:10 +00:00
services.AddTransient<ShellPage>();
services.AddTransient<ShellViewModel>();
// Configuration
services.Configure<LocalSettingsOptions>(context.Configuration.GetSection(nameof(LocalSettingsOptions)));
}).
Build();
UnhandledException += App_UnhandledException;
}
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
// TODO: Log and handle exceptions as appropriate.
// https://docs.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.unhandledexception.
}
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);
await App.GetService<IActivationService>().ActivateAsync(args);
}
2024-07-19 07:58:15 +00:00
private Mutex mutex;
private void InitializeMutex()
{
// Mutex name should be unique for your application.
string mutexName = "Global\\MyWinUIAppMutex";
bool createdNew;
// Try to create the Mutex. If it fails, another instance is already running.
mutex = new Mutex(true, mutexName, out createdNew);
if (!createdNew)
{
// Another instance is already running.
Environment.Exit(0); // Exit the current application.
}
// Clean up the Mutex when the application exits.
//Current.Exiting += (sender, args) =>
//{
// mutex.Close();
// mutex.Dispose();
//};
}
2024-07-14 11:24:10 +00:00
}