Skip to content
Back to skills

Configuring Avalonia Dependency Injection

ASecurity

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

  • 207 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added February 7, 2026
devops

Security analysis

A100/100

Pro scans all 9 files and shows the line behind each finding

Scanned February 10, 2026

npx -y skills add NeverSight/skills_feed --skill configuring-avalonia-dependency-injection --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Configuring Avalonia Dependency Injection?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Configuring Avalonia Dependency Injection
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/neversight-configuring-avalonia-dependency-injection/badge)](https://www.skillsdirectory.com/skills/neversight-configuring-avalonia-dependency-injection)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: configuring-avalonia-dependency-injection
description: "Configures GenericHost and Dependency Injection in AvaloniaUI applications. Use when setting up DI container, registering services, or implementing IoC patterns in AvaloniaUI projects."
---

# 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:

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

Files in this skill

  • SKILL.md2.3 KB
  • description_cn.txt155 B
  • description_de.txt223 B
  • description_en.txt185 B
  • description_es.txt199 B
  • description_fr.txt235 B
  • description_ja.txt275 B
  • description_ko.txt206 B
  • description_tw.txt155 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…