WIn_RustTools/RustTools/App.xaml.cs

165 lines
6.1 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.Contracts.Services;
using RustTools.Models;
using RustTools.Services;
using RustTools.ViewModels;
using RustTools.Views;
using RustTools.muqing;
using RustTools.Activation;
using System.Diagnostics;
using RustTools.WindowUI;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.AppLifecycle;
using Windows.ApplicationModel;
using Windows.Management.Core;
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 : Microsoft.UI.Xaml.Application
2024-07-14 11:24:10 +00:00
{
// 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 UIElement? AppTitlebar { get; set; }
2024-07-29 01:10:42 +00:00
public static WindowEx MainWindow { get; } = new MainWindow();
2024-07-14 11:24:10 +00:00
public App()
{
2024-08-04 02:31:05 +00:00
InitializeComponent();
if (!Directory.Exists(wj.CD))
{
Directory.CreateDirectory(wj.CD);
}
// 注册激活事件处理程序
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<Microsoft.UI.Xaml.LaunchActivatedEventArgs>, DefaultActivationHandler>();
2024-07-14 11:24:10 +00:00
// 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<ConcernPage>();
2024-07-15 05:22:53 +00:00
services.AddTransient<RankingViewModel>();
services.AddTransient<RankingPage>();
services.AddTransient<UserViewModel>();
services.AddTransient<UserPage>();
//模组碎片
services.AddTransient<ModuleViewModel>();
services.AddTransient<ModulePage>();
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(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
2024-07-14 11:24:10 +00:00
{
base.OnLaunched(args);
var activatedEventArgs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
if (activatedEventArgs.Kind == Microsoft.Windows.AppLifecycle.ExtendedActivationKind.File)
{
gj.sc("打开文件");
if (activatedEventArgs.Kind == ExtendedActivationKind.File)
{
if (activatedEventArgs.Data is IFileActivatedEventArgs fileArgs)
{
var files = fileArgs.Files;
// 处理打开的文件
foreach (var file in files)
{
var storageFile = file as Windows.Storage.StorageFile;
if (storageFile != null)
{
gj.sc(storageFile.Path);
var importModule=new ImportModule(storageFile.Path);
importModule.Activate();
// 读取或处理文件内容
}
}
}
}
return;
}
// If this is the first instance launched, then register it as the "main" instance.
// If this isn't the first instance launched, then "main" will already be registered,
// so retrieve it.
var mainInstance = Microsoft.Windows.AppLifecycle.AppInstance.FindOrRegisterForKey("main");
// If the instance that's executing the OnLaunched handler right now
// isn't the "main" instance.
if (!mainInstance.IsCurrent)
{
// Redirect the activation (and args) to the "main" instance, and exit.
await mainInstance.RedirectActivationToAsync(activatedEventArgs);
System.Diagnostics.Process.GetCurrentProcess().Kill();
return;
}
else
{
//MainWindow.Activate();
await App.GetService<IActivationService>().ActivateAsync(args);
}
2024-07-14 11:24:10 +00:00
}
}