InkForge/app/InkForge.Desktop/Program.cs
2024-02-09 01:43:27 +01:00

68 lines
1.7 KiB
C#

using Avalonia;
using Avalonia.ReactiveUI;
using Avalonia.Threading;
using InkForge.Common;
using InkForge.Common.ViewModels;
using InkForge.Data;
using InkForge.Desktop.Views;
using Microsoft.Extensions.DependencyInjection;
using ReactiveUI;
using Splat;
using Splat.Microsoft.Extensions.DependencyInjection;
static class Program
{
[STAThread]
public static void Main(string[] args)
=> BuildAvaloniaApp()
.UseMicrosoftDependencyInjection()
.StartWithClassicDesktopLifetime(args);
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI()
.WithInterFont()
.LogToTrace();
private static void ConfigureServices(IServiceCollection services)
{
services.UseMicrosoftDependencyResolver();
var mutableResolver = Locator.CurrentMutable;
mutableResolver.InitializeSplat();
mutableResolver.InitializeReactiveUI();
services.AddInkForge();
services.AddTransient<IViewFor<AppViewModel>, MainWindow>();
}
private static void OnSetup(this IServiceCollection services, AppBuilder appBuilder)
{
var dispatcher = Dispatcher.UIThread;
var app = appBuilder.Instance!;
services
.AddSingleton(app)
.AddSingleton(app.ApplicationLifetime!)
.AddSingleton(app.PlatformSettings!)
.AddSingleton(dispatcher);
var serviceProvider = services.BuildServiceProvider();
serviceProvider.UseMicrosoftDependencyResolver();
app.SetValue(App.ServiceProviderProperty, serviceProvider);
dispatcher.ShutdownFinished += (_, _) => serviceProvider.Dispose();
}
private static AppBuilder UseMicrosoftDependencyInjection(this AppBuilder builder)
{
ServiceCollection services = [];
ConfigureServices(services);
builder.AfterSetup(services.OnSetup);
return builder;
}
}