Skip to content

Commit

Permalink
Integrate dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
acidicMercury8 committed Nov 23, 2024
1 parent 0adaf36 commit 36fe23e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.Props
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<PackageVersion Include="Avalonia.ReactiveUI" Version="11.2.1" />
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.2.1" />
<PackageVersion Include="Avalonia.Desktop" Version="11.2.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>
</Project>
36 changes: 28 additions & 8 deletions src/Dex.AnomalyCustom.Launcher.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -18,17 +25,30 @@ public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainViewModel()
};
_serviceProvider = new ServiceCollection()
.AddSingleton<MainWindow>()
.AddViews()
.AddViewModels()
.BuildServiceProvider();

var mainViewModel = _serviceProvider.GetRequiredService<MainViewModel>();
var mainWindow = _serviceProvider.GetRequiredService<MainWindow>();
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<MainViewModel>();
var mainView = _serviceProvider.GetRequiredService<MainView>();
mainView.DataContext = mainViewModel;

singleViewPlatform.MainView = mainView;
}

base.OnFrameworkInitializationCompleted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

<ItemGroup>
<PackageReference Include="Avalonia" />
<PackageReference Include="Avalonia.Diagnostics" Condition="'$(Configuration)' == 'Debug'"/>
<PackageReference Include="Avalonia.Diagnostics" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="Avalonia.Fonts.Inter" />
<PackageReference Include="Avalonia.ReactiveUI" />
<PackageReference Include="Avalonia.Themes.Fluent" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -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<MainView>();

serviceCollection.AddTransient<SettingsView>();

return serviceCollection;
}

public static IServiceCollection AddViewModels(this IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<MainViewModel>();

serviceCollection.AddTransient<SettingsViewModel>();

return serviceCollection;
}
}

0 comments on commit 36fe23e

Please sign in to comment.