Lifter.Avalonia
2.0.1
dotnet add package Lifter.Avalonia --version 2.0.1
NuGet\Install-Package Lifter.Avalonia -Version 2.0.1
<PackageReference Include="Lifter.Avalonia" Version="2.0.1" />
<PackageVersion Include="Lifter.Avalonia" Version="2.0.1" />
<PackageReference Include="Lifter.Avalonia" />
paket add Lifter.Avalonia --version 2.0.1
#r "nuget: Lifter.Avalonia, 2.0.1"
#:package Lifter.Avalonia@2.0.1
#addin nuget:?package=Lifter.Avalonia&version=2.0.1
#tool nuget:?package=Lifter.Avalonia&version=2.0.1
Lifter.Avalonia
Avalonia UI 12 integration for Lifter, providing HostedApplication support with dependency injection, configuration, IHostedService lifecycle management, and the cross-platform IDialogService.
Features
✅ Dependency Injection - Full IServiceCollection and IServiceProvider support
✅ Configuration - IConfiguration with JSON, environment variables, and user secrets
✅ IHostedService Support - Background services managed by application lifecycle
✅ IDialogService - Native Avalonia dialog windows with full override points
✅ Cross-Platform - Proper Desktop (Window) and Mobile (SingleView) handling
✅ Splash Screen - Optional splash screen during initialization
✅ Customizable - Configurable window settings and lifecycle hooks
Installation
dotnet add package Lifter.Avalonia
Quick Start
1. Create Your Main View
using Avalonia.Controls;
using Lifter.Avalonia;
public partial class MainView : UserControl, IHostedView
{
public MainView()
{
InitializeComponent();
}
}
2. Create Your App Class
using Lifter.Avalonia;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
public class App : HostedApplication<MainView>
{
protected override void ConfigureServices(
IServiceCollection services,
IConfiguration configuration)
{
// Configure window settings
services.ConfigureWindow(config =>
{
config.Title = "My Avalonia App";
config.Width = 1024;
config.Height = 768;
});
// Register your services
services.AddSingleton<IMyService, MyService>();
// Register background services
services.AddHostedService<MyBackgroundService>();
}
}
3. Update App.axaml
<avalonia:HostedApplication
xmlns="https://github.com/avaloniaui"
xmlns:avalonia="using:Lifter.Avalonia"
x:Class="MyApp.App">
</avalonia:HostedApplication>
Advanced Usage
Custom Initialization
Override OnHostInitializedAsync for custom initialization logic:
protected override async Task OnHostInitializedAsync()
{
// Perform database migration, load cached data, etc.
var dbContext = Services.GetRequiredService<AppDbContext>();
await dbContext.Database.MigrateAsync();
}
Splash Screen
Override CreateSplashScreen to show a custom splash screen:
protected override Control? CreateSplashScreen()
{
return new SplashScreenView();
}
Background Services
Implement IHostedService for long-running background tasks:
public class DataSyncService : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
{
// Start background task
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
// Clean up
return Task.CompletedTask;
}
}
// Register in ConfigureServices
services.AddHostedService<DataSyncService>();
Platform Support
- ✅ Desktop (Windows, macOS, Linux) - Creates a
Windowwith your view - ✅ Mobile (iOS, Android via .NET MAUI) - Direct view assignment
- ✅ .NET 10 — targets
net10.0 - ✅ Avalonia 12.0
How It Works
Desktop vs Mobile
Desktop (IClassicDesktopStyleApplicationLifetime):
var mainWindow = new Window
{
Content = mainView, // Your view becomes window content
Title = "App",
Width = 1200,
Height = 960
};
desktop.MainWindow = mainWindow;
mainWindow.Show();
Mobile (ISingleViewApplicationLifetime):
singleView.MainView = mainView; // Direct view assignment
Initialization Sequence
- Build Host - Create
IHostwith DI and configuration - Show Splash - Optional splash screen
- Async Init - Run
OnHostInitializedAsyncon background thread - Setup View - Create Window (Desktop) or set MainView (Mobile) on UI thread
- Start Services - Start all
IHostedServiceinstances
Architecture
HostedApplication<TMainView>
├── HostApplicationBuilder (Microsoft.Extensions.Hosting)
├── IServiceProvider (Dependency Injection)
├── IConfiguration (Configuration)
└── HostManager (Lifter.Core - manages IHostedService lifecycle)
License
MIT - See LICENSE file for details
Related Packages
- Lifter.Core - Core abstractions,
HostManager, andIDialogServiceinterfaces - Lifter.Maui - .NET MAUI integration for Lifter
- Lifter.Blazor - Blazor WebAssembly integration for Lifter
IDialogService
AvaloniaDialogService shows native Avalonia Window dialogs parented to the desktop main window.
Registration
// In ConfigureServices (App.cs)
services.AddAvaloniaDialogService();
Usage
public class MainView : UserControl, IHostedView
{
private readonly IDialogService _dialogs;
public MainView(IDialogService dialogs)
{
_dialogs = dialogs;
}
private async Task OnDeleteClicked()
{
var result = await _dialogs.ShowConfirmAsync(
"Delete item",
"Are you sure?",
new DialogOptions { ButtonSet = DialogButtonSet.YesNo });
if (result.Confirmed)
{
// delete logic
}
}
}
Custom Override
Subclass AvaloniaDialogService and register it to customise any aspect of the dialog window:
public class MyDialogService : AvaloniaDialogService
{
// Override to change which window owns the dialog
protected override Window? GetOwnerWindow() { ... }
// Override to fully restyle the dialog window
protected override Window BuildDialogWindow(string title, DialogOptions options) { ... }
// Override to change button layout
protected override Panel BuildButtonPanel(DialogOptions options,
TaskCompletionSource<DialogResult> tcs, Window dialog) { ... }
}
// Register the custom implementation
services.AddAvaloniaDialogService<MyDialogService>();
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Avalonia (>= 12.0.2)
- Lifter.Core (>= 2.0.1)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 10.0.7)
- Microsoft.Extensions.Configuration.Json (>= 10.0.7)
- Microsoft.Extensions.Hosting (>= 10.0.7)
- ProDiagnostics (>= 12.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v2.0.1: Added NuGet README files for all packages. v2.0.0: Migrated to .NET 10 and Avalonia 12; replaced Avalonia.Diagnostics with ProDiagnostics; added AvaloniaDialogService.