ReactiveUI.Avalonia.Autofac
12.1.2
Prefix Reserved
dotnet add package ReactiveUI.Avalonia.Autofac --version 12.1.2
NuGet\Install-Package ReactiveUI.Avalonia.Autofac -Version 12.1.2
<PackageReference Include="ReactiveUI.Avalonia.Autofac" Version="12.1.2" />
<PackageVersion Include="ReactiveUI.Avalonia.Autofac" Version="12.1.2" />
<PackageReference Include="ReactiveUI.Avalonia.Autofac" />
paket add ReactiveUI.Avalonia.Autofac --version 12.1.2
#r "nuget: ReactiveUI.Avalonia.Autofac, 12.1.2"
#:package ReactiveUI.Avalonia.Autofac@12.1.2
#addin nuget:?package=ReactiveUI.Avalonia.Autofac&version=12.1.2
#tool nuget:?package=ReactiveUI.Avalonia.Autofac&version=12.1.2
<br> <a href="https://github.com/reactiveui/reactiveui"> <img width="160" height="160" src="https://raw.githubusercontent.com/reactiveui/styleguide/master/logo/main.png"> </a> <br>
ReactiveUI for Avalonia UI
This package provides ReactiveUI bindings and helpers for the Avalonia UI framework, enabling you to build composable, cross-platform model-view-viewmodel (MVVM) applications for Windows, macOS, and Linux.
Packages
Install the packages that match your preferred dependency injection container. The core package is always required.
All libraries target .NET 8, 9, 10, and 11. The core and DI packages also have .Reactive variants for applications using System.Reactive; new applications can use the lean packages backed by ReactiveUI.Primitives.
Runnable showcase
The example application demonstrates navigation, reactive bindings, commands, activation, interactions, reusable metric cards, and live local process measurements using the lean packages.
From src, run:
dotnet run --project examples/ReactiveUI.Avalonia.Example/ReactiveUI.Avalonia.Example.csproj -c Release
The example includes headless TUnit tests so its behavior can be checked alongside the package tests.
Recommended setup (ReactiveUIBuilder)
The recommended approach for new projects is to use the ReactiveUIBuilder via the UseReactiveUI and UseReactiveUIWith... extensions. This ensures consistent registration of schedulers, activation/binding hooks, and view discovery.
Namespaces to import in your startup:
using Avalonia; // AppBuilder
using ReactiveUI.Avalonia; // UseReactiveUI, RegisterReactiveUIViews* (core)
using ReactiveUI.Avalonia.Splat; // Autofac, DryIoc, Ninject, Microsoft.Extensions.DependencyInjection integrations
Minimal setup (no external DI container):
public static class Program
{
public static void Main(string[] args) => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
public static AppBuilder BuildAvaloniaApp() => AppBuilder
.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI(rxui =>
{
// Optional: add custom registration here via rxui.WithRegistration(...)
})
.RegisterReactiveUIViewsFromEntryAssembly();
}
With Autofac:
public static AppBuilder BuildAvaloniaApp() => AppBuilder
.Configure<App>()
.UsePlatformDetect()
.UseReactiveUIWithAutofac(
container =>
{
// Register your services/view models
// container.RegisterType<MainViewModel>();
},
withResolver: resolver =>
{
// Optional: access the Autofac resolver/lifetime scope
},
withReactiveUIBuilder: rxui =>
{
// Optional: add ReactiveUI customizations
})
.RegisterReactiveUIViewsFromEntryAssembly();
With DryIoc:
public static AppBuilder BuildAvaloniaApp() => AppBuilder
.Configure<App>()
.UsePlatformDetect()
.UseReactiveUIWithDryIoc(
container =>
{
// container.Register<MainViewModel>(Reuse.Singleton);
},
withReactiveUIBuilder: rxui =>
{
// Optional ReactiveUI customizations
})
.RegisterReactiveUIViewsFromEntryAssembly();
With Microsoft.Extensions.DependencyInjection:
public static AppBuilder BuildAvaloniaApp() => AppBuilder
.Configure<App>()
.UsePlatformDetect()
.UseReactiveUIWithMicrosoftDependencyResolver(
services =>
{
// services.AddSingleton<MainViewModel>();
},
withResolver: sp =>
{
// Optional: access ServiceProvider
})
;
Note: UseReactiveUIWithMicrosoftDependencyResolver(...) builds the ServiceProvider during setup, so make sure you register all services (including any IViewFor<T> views) in the services => { ... } callback.
With Ninject:
public static AppBuilder BuildAvaloniaApp() => AppBuilder
.Configure<App>()
.UsePlatformDetect()
.UseReactiveUIWithNinject(
kernel =>
{
// kernel.Bind<MainViewModel>().ToSelf().InSingletonScope();
},
withReactiveUIBuilder: rxui =>
{
// Optional ReactiveUI customizations
})
.RegisterReactiveUIViewsFromEntryAssembly();
Notes
UseReactiveUIregisters the package-owned Avalonia scheduler fromReactiveUI.Primitives.Avalonia(orReactiveUI.Primitives.Avalonia.Reactive) as ReactiveUI's main-thread sequencer/scheduler, and registers the Avalonia-specific activation and binding services.RegisterReactiveUIViewsFromEntryAssembly()scans your entry assembly and registers any types implementingIViewFor<TViewModel>for view location/navigation.- For existing apps, you can keep using
UseReactiveUI()without a DI container and register services intoSplatdirectly if you prefer.
Manual setup (without container mixins)
You can configure a custom container using the generic UseReactiveUIWithDIContainer if you don’t use one of the provided integrations:
AppBuilder
.Configure<App>()
.UseReactiveUIWithDIContainer(
containerFactory: () => new MyContainer(),
containerConfig: container =>
{
// configure container
},
dependencyResolverFactory: container => new MySplatResolver(container))
.RegisterReactiveUIViewsFromEntryAssembly();
Quick example: first reactive view
// View model
using ReactiveUI;
public class MyViewModel : ReactiveObject
{
private string _greeting = "Hello, Reactive World!";
public string Greeting
{
get => _greeting;
set => this.RaiseAndSetIfChanged(ref _greeting, value);
}
}
<UserControl x:Class="MyAvaloniaApp.Views.MainView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:rxui="using:ReactiveUI.Avalonia">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock x:Name="GreetingTextBlock" FontSize="24"/>
</StackPanel>
</UserControl>
// MainView.axaml.cs
using ReactiveUI;
using ReactiveUI.Avalonia;
using System.Reactive.Disposables;
public partial class MainView : ReactiveUserControl<MyViewModel>
{
public MainView()
{
InitializeComponent();
ViewModel = new MyViewModel();
this.WhenActivated(disposables =>
{
this.OneWayBind(ViewModel, vm => vm.Greeting, v => v.GreetingTextBlock.Text)
.DisposeWith(disposables);
});
}
}
API reference
ReactiveUI.Avalonia (core)
Key extension methods on AppBuilder:
UseReactiveUI()— initialize ReactiveUI for Avalonia (scheduler, activation, bindings)UseReactiveUI(Action<ReactiveUIBuilder>)— initialize with theReactiveUIBuilderfor additional configurationRegisterReactiveUIViews(params Assembly[])— scan and register views implementingIViewFor<T>RegisterReactiveUIViewsFromEntryAssembly()— convenience overload to scan the entry assemblyRegisterReactiveUIViewsFromAssemblyOf<TMarker>()— scan a specific assemblyUseReactiveUIWithDIContainer<TContainer>(...)— bring-your-own container integration via anIDependencyResolver
Important types registered by default:
IActivationForViewFetcher?AvaloniaActivationForViewFetcherIPropertyBindingHook?AutoDataTemplateBindingHookICreatesCommandBinding?AvaloniaCreatesCommandBindingICreatesObservableForProperty?AvaloniaObjectObservableForProperty- The main-thread sequencer/scheduler set to the matching
ReactiveUI.Primitives.Avalonia.*AvaloniaScheduler.Instance
Controls and helpers:
RoutedViewHost— view host that displays the view for the currentRoutingStateReactiveUserControl<TViewModel>,ReactiveWindow<TViewModel>— base classes for reactive views
ReactiveUI.Avalonia.Autofac
Extension methods on AppBuilder (namespace Avalonia.ReactiveUI.Splat):
UseReactiveUIWithAutofac(Action<ContainerBuilder> containerConfig, Action<AutofacDependencyResolver>? withResolver = null)UseReactiveUIWithAutofac(Action<ContainerBuilder> containerConfig, Action<AutofacDependencyResolver>? withResolver = null, Action<ReactiveUIBuilder>? withReactiveUIBuilder = null)
What it does:
- Sets up
Splatwith Autofac, initializes ReactiveUI for Avalonia, builds your container, and optionally exposes the Autofac resolver.
ReactiveUI.Avalonia.DryIoc
Extension methods on AppBuilder (namespace ReactiveUI.Avalonia.Splat):
UseReactiveUIWithDryIoc(Action<Container> containerConfig)UseReactiveUIWithDryIoc(Action<Container> containerConfig, Action<ReactiveUIBuilder>? withReactiveUIBuilder = null)
What it does:
- Wires
Splatto DryIoc, initializes ReactiveUI for Avalonia, and lets you register services on the container.
ReactiveUI.Avalonia.Microsoft.Extensions.DependencyInjection
Extension methods on AppBuilder (namespace ReactiveUI.Avalonia.Splat):
UseReactiveUIWithMicrosoftDependencyResolver(Action<IServiceCollection> containerConfig, Action<IServiceProvider?>? withResolver = null)UseReactiveUIWithMicrosoftDependencyResolver(Action<IServiceCollection> containerConfig, Action<IServiceProvider?>? withResolver = null, Action<ReactiveUIBuilder>? withReactiveUIBuilder = null)
What it does:
- Sets up
SplatusingIServiceCollection/ServiceProvider, initializes ReactiveUI for Avalonia, and exposes the built provider if you need it.
ReactiveUI.Avalonia.Ninject
Extension methods on AppBuilder (namespace ReactiveUI.Avalonia.Splat):
UseReactiveUIWithNinject(Action<StandardKernel> containerConfig)UseReactiveUIWithNinject(Action<StandardKernel> containerConfig, Action<ReactiveUIBuilder>? withReactiveUIBuilder = null)
What it does:
- Wires
Splatto Ninject, initializes ReactiveUI for Avalonia, and lets you configure bindings on the kernel.
Tutorial: Mastering ReactiveUI with Avalonia
Welcome to the ReactiveUI.Avalonia guide! This tutorial walks you through setting up an Avalonia app with ReactiveUI. We start with the basics and build up to a reactive application.
ReactiveUI.Avalonia provides the necessary bindings and helpers to seamlessly integrate the ReactiveUI MVVM framework with your Avalonia projects, enabling elegant, testable, and maintainable code.
Chapter 1: Getting Started - Your First Reactive View
1. Installation
Add the ReactiveUI.Avalonia package to your Avalonia application project file.
<PackageReference Include="ReactiveUI.Avalonia" Version="11.3.0" />
2. Initialization (recommended)
Use the builder-based setup shown above (see "Recommended setup"). For a minimal variant:
AppBuilder.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI()
.RegisterReactiveUIViewsFromEntryAssembly();