v2rayN/v2rayN/v2rayN.Desktop/App.axaml.cs
2dust 1a6fbf782d
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release macOS / build (Release) (push) Waiting to run
release Windows desktop (Avalonia UI) / build (Release) (push) Waiting to run
release Windows / build (Release) (push) Waiting to run
Using RxApp replace ViewAction
2025-09-02 17:12:38 +08:00

80 lines
2.5 KiB
C#

using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using ServiceLib.Manager;
using Splat;
using v2rayN.Desktop.Common;
using v2rayN.Desktop.Views;
namespace v2rayN.Desktop;
public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
var ViewModel = new StatusBarViewModel(null);
Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(StatusBarViewModel));
DataContext = ViewModel;
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
AppManager.Instance.InitComponents();
desktop.Exit += OnExit;
desktop.MainWindow = new MainWindow();
}
base.OnFrameworkInitializationCompleted();
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject != null)
{
Logging.SaveLog("CurrentDomain_UnhandledException", (Exception)e.ExceptionObject);
}
}
private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
Logging.SaveLog("TaskScheduler_UnobservedTaskException", e.Exception);
}
private void OnExit(object? sender, ControlledApplicationLifetimeExitEventArgs e)
{
}
private async void MenuAddServerViaClipboardClick(object? sender, EventArgs e)
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
if (desktop.MainWindow != null)
{
var clipboardData = await AvaUtils.GetClipboardData(desktop.MainWindow);
if (clipboardData.IsNullOrEmpty())
{
return;
}
var service = Locator.Current.GetService<MainWindowViewModel>();
if (service != null)
{
_ = service.AddServerViaClipboardAsync(clipboardData);
}
}
}
}
private async void MenuExit_Click(object? sender, EventArgs e)
{
await AppManager.Instance.AppExitAsync(false);
AppManager.Instance.Shutdown(true);
}
}