configuring-avalonia-dependency-injection
6.6 Dependency Injection and GenericHost Usage
Apply the same GenericHost pattern in AvaloniaUI as in WPF
Project Structure
The templates folder contains a .NET 9 AvaloniaUI project example.
templates/
├── AvaloniaDISample.App/ ← Avalonia Application Project
│ ├── Views/
│ │ ├── MainWindow.axaml
│ │ └── MainWindow.axaml.cs
│ ├── App.axaml
│ ├── App.axaml.cs
│ ├── Program.cs
│ ├── GlobalUsings.cs
│ └── AvaloniaDISample.App.csproj
└── AvaloniaDISample.ViewModels/ ← ViewModel Class Library (UI framework independent)
├── MainViewModel.cs
├── GlobalUsings.cs
└── AvaloniaDISample.ViewModels.csproj
App.axaml.cs Example:
// App.axaml.cs
namespace MyApp;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
public partial class App : Application
{
private IHost? _host;
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
// Create GenericHost and register services
_host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
// Register services
services.AddSingleton<IUserRepository, UserRepository>();
services.AddSingleton<IUserService, UserService>();
services.AddTransient<IDialogService, DialogService>();
// Register ViewModels
services.AddTransient<MainViewModel>();
// Register Views
services.AddSingleton<MainWindow>();
})
.Build();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = _host.Services.GetRequiredService<MainWindow>();
}
base.OnFrameworkInitializationCompleted();
}
}
More from christian289/dotnet-with-claudecode
converting-html-css-to-wpf-xaml
Converts HTML/CSS to WPF CustomControl XAML with correct patterns and common pitfall solutions. Use when transforming web designs to WPF, converting CSS animations to Storyboards, implementing CSS border-radius clipping, CSS pseudo-elements (::before/::after), or CSS transforms in XAML.
56publishing-wpf-apps
Guides WPF application publishing and installer options. Use when user mentions publish, deploy, release, packaging, or installer to help choose deployment method and installer technology.
14using-avalonia-collectionview
Provides CollectionView alternatives for AvaloniaUI using DataGridCollectionView and ReactiveUI. Use when filtering, sorting, or grouping collections in AvaloniaUI applications.
9designing-avalonia-customcontrol-architecture
Defines the basic solution structure for AvaloniaUI Desktop Applications using CustomControl. Use when creating new AvaloniaUI projects or designing stand-alone control styles with ControlTheme.
9using-xaml-property-element-syntax
Converts long inline XAML bindings to Property Element Syntax for better readability. Use when XAML binding expressions become too long or complex.
8managing-styles-resourcedictionary
Manages WPF Style definitions and ResourceDictionary patterns including BasedOn inheritance and resource merging. Use when building theme systems, organizing resources, or choosing between StaticResource and DynamicResource.
8