smithery.ai

configuring-avalonia-dependency-injection

Configures GenericHost and Dependency Injection in AvaloniaUI applications. Use when setting up DI container, registering services, or implementing IoC patterns in AvaloniaUI projects.

First seen Mar 23, 2026

Installation

$ npx skills add https://smithery.ai

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from smithery.ai · top by installs.

npx skills add https://smithery.ai

Browse all from smithery.ai

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 2,352 B
  • docs SUMMARY.md 233 B

History

  1. First seen on skills.sh
  2. First recorded snapshot · 1 installs

SKILL.md

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();
    }
}