ReactiveUI
ReactiveUI is used in our examples, but it's not required. Avalonia supports any MVVM framework or your own custom solutions.
These pages explain how Avalonia UI uses a version of the open-source ReactiveUI framework to make it easier to implement the MVVM pattern in your application.
ReactiveUI is an advanced, composable, functional reactive model-view-viewmodel (MVVM) framework for all .NET platforms. It was inspired by the functional reactive programming paradigm.
For a full technical background on functional reactive programming, see the Wikipedia article here.
Avalonia UI ships with its own fork of ReactiveUI in the Avalonia.ReactiveUI
NuGet package.
To use ReactiveUI and the MVVM pattern in your Avalonia UI application, add the package to your project using the NuGet package manager (as above), or execute the following CLI command:
dotnet add package Avalonia.ReactiveUI
For detailed information about ReactiveUI itself, see the website https://reactiveui.net/
For more background about the MVVM pattern, see the Microsoft article here.
The package includes helpers specifically for Avalonia UI to handle the ReactiveUI tasks of view model-based routing, view activation and scheduling. (see the above reference for full details of these tasks).
If you start your application from the Avalonia MVVM Application solution template; then you will already have the ReactiveUI package installed and configured.
Configure to Use ReactiveUI
Having installed the NuGet package, you must configure the application Program
class to use it. Check that you call the UseReactiveUI()
method in the AppBuilder
code.
For example, if you use the Avalonia MVVM Application solution template, it will automatically add the NuGet package, and then add the code:
internal class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToTrace()
.UseReactiveUI();
}
In the following pages, you will learn how ReactiveUI works with Avalonia UI to allow you to implement the following application scenarios:
- Data Binding a Reactive View Model
- View Activation
- Routing
- Data Persistence
- Binding to Sorted/Filtered Data