InkForge/app/InkForge.Desktop/Program.cs

68 lines
1.8 KiB
C#
Raw Normal View History

2024-02-09 01:23:38 +01:00
using Avalonia;
2024-02-11 02:39:36 +01:00
using Avalonia.Controls.ApplicationLifetimes;
2024-02-08 00:54:45 +01:00
using Avalonia.ReactiveUI;
using Avalonia.Threading;
using InkForge.Common;
2024-02-09 01:43:27 +01:00
using InkForge.Common.ViewModels;
using InkForge.Desktop.Views;
2024-02-08 00:54:45 +01:00
2024-02-11 02:39:36 +01:00
using Microsoft.Extensions.Configuration;
2024-02-08 00:54:45 +01:00
using Microsoft.Extensions.DependencyInjection;
using ReactiveUI;
static class Program
{
2024-02-11 02:39:36 +01:00
private static readonly ConfigurationManager Configuration = new();
2024-02-08 00:54:45 +01:00
[STAThread]
public static void Main(string[] args)
=> BuildAvaloniaApp()
2024-02-08 01:22:32 +01:00
.UseMicrosoftDependencyInjection()
2024-02-11 02:39:36 +01:00
.StartWithClassicDesktopLifetime(args, WithMicrosoftDependencyInjection);
private static void WithMicrosoftDependencyInjection(IClassicDesktopStyleApplicationLifetime lifetime)
{
Configuration.AddCommandLine(lifetime.Args ?? []);
}
2024-02-08 00:54:45 +01:00
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI()
2024-02-09 01:23:38 +01:00
.WithInterFont()
2024-02-08 00:54:45 +01:00
.LogToTrace();
private static void ConfigureServices(IServiceCollection services)
{
2024-02-09 01:43:27 +01:00
services.AddTransient<IViewFor<AppViewModel>, MainWindow>();
2024-02-08 00:54:45 +01:00
}
private static void OnSetup(this IServiceCollection services, AppBuilder appBuilder)
2024-02-08 00:54:45 +01:00
{
var dispatcher = Dispatcher.UIThread;
var app = appBuilder.Instance!;
services
2024-02-08 00:54:45 +01:00
.AddSingleton(app)
.AddSingleton(app.ApplicationLifetime!)
.AddSingleton(app.PlatformSettings!)
.AddSingleton(dispatcher);
2024-02-11 02:39:36 +01:00
ConfigureServices(services);
var serviceProvider = services.BuildServiceProvider();
app.SetValue(App.ServiceProviderProperty, serviceProvider);
dispatcher.ShutdownFinished += (_, _) => serviceProvider.Dispose();
2024-02-08 00:54:45 +01:00
}
2024-02-08 01:22:32 +01:00
private static AppBuilder UseMicrosoftDependencyInjection(this AppBuilder builder)
2024-02-08 00:54:45 +01:00
{
ServiceCollection services = [];
2024-02-11 02:39:36 +01:00
App.Configure(services, Configuration);
builder.AfterSetup(services.OnSetup);
2024-02-08 00:54:45 +01:00
return builder;
}
}