Files

93 lines
3.2 KiB
C#
Raw Permalink Normal View History

2025-01-14 22:10:16 +03:00
using System.Linq;
2024-12-18 12:37:00 +03:00
using Avalonia;
2025-01-16 21:07:50 +03:00
using Avalonia.Controls;
2024-12-18 12:37:00 +03:00
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
2024-12-21 13:11:30 +03:00
using Microsoft.Extensions.DependencyInjection;
using Nebula.Launcher.MessageBox;
2024-12-18 12:37:00 +03:00
using Nebula.Launcher.Views;
2025-01-05 17:05:23 +03:00
using Nebula.Shared;
2025-05-05 21:28:09 +03:00
using Nebula.Shared.Services;
2024-12-18 12:37:00 +03:00
namespace Nebula.Launcher;
2025-01-14 22:10:16 +03:00
public class App : Application
2024-12-18 12:37:00 +03:00
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
if (!Program.IsNewInstance)
{
IMessageContainerProvider? provider = null;
switch (ApplicationLifetime)
{
case IClassicDesktopStyleApplicationLifetime desktop:
DisableAvaloniaDataAnnotationValidation();
2025-07-14 10:06:38 +03:00
desktop.MainWindow = (Window)(provider = new MessageWindow());
break;
case ISingleViewApplicationLifetime singleViewPlatform:
2025-07-14 10:06:38 +03:00
singleViewPlatform.MainView = (Control)(provider = new MessageView());
break;
}
2025-07-14 10:06:38 +03:00
provider?.ShowMessage(
"Error: An instance of the application is already running. Please close the existing instance before launching a new one.",
"Duplicate instance detected.");
return;
}
2025-01-16 21:07:50 +03:00
if (Design.IsDesignMode)
2024-12-18 12:37:00 +03:00
{
2025-01-16 21:07:50 +03:00
switch (ApplicationLifetime)
{
case IClassicDesktopStyleApplicationLifetime desktop:
DisableAvaloniaDataAnnotationValidation();
desktop.MainWindow = new MainWindow();
break;
case ISingleViewApplicationLifetime singleViewPlatform:
singleViewPlatform.MainView = new MainView();
break;
}
}
else
{
2025-05-05 21:28:09 +03:00
DebugService.DoFileLog = true;
2025-01-16 21:07:50 +03:00
var services = new ServiceCollection();
services.AddAvaloniaServices();
services.AddServices();
services.AddViews();
var serviceProvider = services.BuildServiceProvider();
switch (ApplicationLifetime)
{
case IClassicDesktopStyleApplicationLifetime desktop:
DisableAvaloniaDataAnnotationValidation();
desktop.MainWindow = serviceProvider.GetService<MainWindow>();
break;
case ISingleViewApplicationLifetime singleViewPlatform:
singleViewPlatform.MainView = serviceProvider.GetRequiredService<MainView>();
break;
}
2024-12-18 12:37:00 +03:00
}
2025-01-16 21:07:50 +03:00
2024-12-18 12:37:00 +03:00
base.OnFrameworkInitializationCompleted();
}
private void DisableAvaloniaDataAnnotationValidation()
{
// Get an array of plugins to remove
var dataValidationPluginsToRemove =
BindingPlugins.DataValidators.OfType<DataAnnotationsValidationPlugin>().ToArray();
// remove each entry found
2025-01-14 22:10:16 +03:00
foreach (var plugin in dataValidationPluginsToRemove) BindingPlugins.DataValidators.Remove(plugin);
2024-12-18 12:37:00 +03:00
}
}