configuring-avalonia-dependency-injection

Configures GenericHost and dependency injection in AvaloniaUI applications.

Updated Nov 22, 2023
One-click install
npx skills add https://github.com/parksanghoon-sys/TestCode --skill configuring-avalonia-dependency-injection-parksanghoon-sys
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: configuring-avalonia-dependency-injection
Source: https://github.com/parksanghoon-sys/TestCode/tree/main/src/Modbus/.agents/skills/configuring-avalonia-dependency-injection
Command: npx skills add https://github.com/parksanghoon-sys/TestCode --skill configuring-avalonia-dependency-injection-parksanghoon-sys

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Setting up dependency injection in AvaloniaUI lacks the built-in hosting infrastructure found in ASP.NET, forcing developers to manually wire GenericHost, service registration, and ViewModel resolution into the application lifecycle. ## Core Features & Use Cases - GenericHost Integration: Shows how to create and configure Microsoft.Extensions.Hosting inside App.axaml.cs using Host.CreateDefaultBuilder. - Service and ViewModel Registration: Demonstrates registering services, ViewModels, and Views with singleton and transient lifetimes in the DI container. - Constructor Injection for Views: Provides a working template where MainWindow receives its MainViewModel through constructor injection and sets DataContext. - Use Case: When starting a new .NET 9 AvaloniaUI desktop app with MVVM, use this Skill to scaffold a DI-enabled project structure with a separate UI-independent ViewModels library using CommunityToolkit.Mvvm. ## Quick Start Set up dependency injection with GenericHost in my AvaloniaUI app so MainWindow gets its MainViewModel through constructor injection.

Frequently Asked Questions about configuring-avalonia-dependency-injection

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add dependency injection to an AvaloniaUI app?▼

Add dependency injection to AvaloniaUI by creating a GenericHost with Host.CreateDefaultBuilder in App.axaml.cs, registering services and ViewModels in ConfigureServices, then resolving the MainWindow from the host's service provider during OnFrameworkInitializationCompleted.

How to inject ViewModel into Avalonia window constructor?▼

Register the ViewModel with services.AddTransient and the Window with services.AddSingleton, then add the ViewModel as a constructor parameter in the Window. Set DataContext to the injected ViewModel inside the constructor after InitializeComponent.

Does AvaloniaUI support Microsoft.Extensions.Hosting GenericHost?▼

Yes, AvaloniaUI works with Microsoft.Extensions.Hosting by building the host inside OnFrameworkInitializationCompleted. The same GenericHost pattern used in WPF applies, giving access to configuration, logging, and the DI container.

What service lifetime should ViewModels use in Avalonia DI?▼

ViewModels are typically registered as transient so each Window resolution gets a fresh instance, while shared services like repositories use singleton. The template registers MainViewModel with AddTransient and MainWindow with AddSingleton.

Can ViewModels be in a separate project from the Avalonia app?▼

Yes, ViewModels can live in a UI-framework-independent class library referencing only CommunityToolkit.Mvvm. The Avalonia app project references this library and registers the ViewModels in its DI container, keeping presentation logic testable and portable.