diff --git a/Directory.Packages.Props b/Directory.Packages.Props index 36c62fb..a8202d2 100644 --- a/Directory.Packages.Props +++ b/Directory.Packages.Props @@ -10,5 +10,6 @@ + diff --git a/src/Dex.AnomalyCustom.Launcher.Avalonia/App.axaml.cs b/src/Dex.AnomalyCustom.Launcher.Avalonia/App.axaml.cs index f6654cb..962d395 100644 --- a/src/Dex.AnomalyCustom.Launcher.Avalonia/App.axaml.cs +++ b/src/Dex.AnomalyCustom.Launcher.Avalonia/App.axaml.cs @@ -2,13 +2,20 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; +using Dex.AnomalyCustom.Launcher.Avalonia.Extensions; using Dex.AnomalyCustom.Launcher.Avalonia.ViewModels; using Dex.AnomalyCustom.Launcher.Avalonia.Views; +using Microsoft.Extensions.DependencyInjection; + +using System; + namespace Dex.AnomalyCustom.Launcher.Avalonia; public partial class App : Application { + private IServiceProvider _serviceProvider = null!; + public override void Initialize() { AvaloniaXamlLoader.Load(this); @@ -18,17 +25,30 @@ public override void OnFrameworkInitializationCompleted() { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { - desktop.MainWindow = new MainWindow - { - DataContext = new MainViewModel() - }; + _serviceProvider = new ServiceCollection() + .AddSingleton() + .AddViews() + .AddViewModels() + .BuildServiceProvider(); + + var mainViewModel = _serviceProvider.GetRequiredService(); + var mainWindow = _serviceProvider.GetRequiredService(); + mainWindow.DataContext = mainViewModel; + + desktop.MainWindow = mainWindow; } else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform) { - singleViewPlatform.MainView = new MainView - { - DataContext = new MainViewModel() - }; + _serviceProvider = new ServiceCollection() + .AddViews() + .AddViewModels() + .BuildServiceProvider(); + + var mainViewModel = _serviceProvider.GetRequiredService(); + var mainView = _serviceProvider.GetRequiredService(); + mainView.DataContext = mainViewModel; + + singleViewPlatform.MainView = mainView; } base.OnFrameworkInitializationCompleted(); diff --git a/src/Dex.AnomalyCustom.Launcher.Avalonia/Dex.AnomalyCustom.Launcher.Avalonia.csproj b/src/Dex.AnomalyCustom.Launcher.Avalonia/Dex.AnomalyCustom.Launcher.Avalonia.csproj index b59457c..219e1fa 100644 --- a/src/Dex.AnomalyCustom.Launcher.Avalonia/Dex.AnomalyCustom.Launcher.Avalonia.csproj +++ b/src/Dex.AnomalyCustom.Launcher.Avalonia/Dex.AnomalyCustom.Launcher.Avalonia.csproj @@ -12,9 +12,10 @@ - + + diff --git a/src/Dex.AnomalyCustom.Launcher.Avalonia/Extensions/ServiceCollectionExtensions.cs b/src/Dex.AnomalyCustom.Launcher.Avalonia/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..b5c53f8 --- /dev/null +++ b/src/Dex.AnomalyCustom.Launcher.Avalonia/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,27 @@ +using Dex.AnomalyCustom.Launcher.Avalonia.ViewModels; +using Dex.AnomalyCustom.Launcher.Avalonia.Views; + +using Microsoft.Extensions.DependencyInjection; + +namespace Dex.AnomalyCustom.Launcher.Avalonia.Extensions; + +internal static class ServiceCollectionExtensions +{ + public static IServiceCollection AddViews(this IServiceCollection serviceCollection) + { + serviceCollection.AddSingleton(); + + serviceCollection.AddTransient(); + + return serviceCollection; + } + + public static IServiceCollection AddViewModels(this IServiceCollection serviceCollection) + { + serviceCollection.AddSingleton(); + + serviceCollection.AddTransient(); + + return serviceCollection; + } +}