Umbra.Avalonia.Router 2.0.0

Suggested Alternatives

Umbra.Router.Core

dotnet add package Umbra.Avalonia.Router --version 2.0.0
                    
NuGet\Install-Package Umbra.Avalonia.Router -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Umbra.Avalonia.Router" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Umbra.Avalonia.Router" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Umbra.Avalonia.Router" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Umbra.Avalonia.Router --version 2.0.0
                    
#r "nuget: Umbra.Avalonia.Router, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Umbra.Avalonia.Router@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Umbra.Avalonia.Router&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Umbra.Avalonia.Router&version=2.0.0
                    
Install as a Cake Tool

Umbra.Avalonia.Router

Cross-platform routing library for AvaloniaUI.

This project started as a fork of Sandreas.Avalonia.SimpleRouter, but has since evolved — the API and internals are now significantly different.


New: NavigationContext + IRoutePage

Optional modern API built around:

  • NavigationContext
  • strongly-typed route pages via IRoutePage (ViewModel)
  • async navigation hooks
  • natural integration with IServiceCollection
  • container-agnostic (DryIoc / Autofac / Microsoft DI / etc)

Install

dotnet add package Umbra.Avalonia.Router

Features

  • lightweight
  • IoC / DI friendly
  • automatic parameter injection into ViewModels
  • navigation history (Back / Forward)
  • extensible
  • new: NavigationContext + IRoutePage API

New API Overview

Base Page

public abstract class ViewModelBasePage : PageRouterBase
{
    public override Task OnNavigatedToAsync(CancellationToken ctx)
      => Task.CompletedTask;
}

ViewModels

public partial class HomeViewModel : ViewModelBasePage
{
    private readonly RouterHistory<ViewModelBasePage> _router;

    public HomeViewModel(RouterHistory<ViewModelBasePage> router)
        => _router = router;

    [RelayCommand]
    public void NavigateToStore()
        => _router.Navigate("store?query=games&page=1&size=20",
                            "My App Store",
                            new SessionParams(1, DateTime.UtcNow));
}

public partial class StoreViewModel : ViewModelBasePage
{
    [ObservableProperty] private string _query;
    [ObservableProperty] private int _page;
    [ObservableProperty] private int _size;

    private SessionParams _body;
    private readonly RouterHistory<ViewModelBasePage> _router;

    public StoreViewModel(RouterHistory<ViewModelBasePage> router)
        => _router = router;

    public override Task OnNavigatedToAsync(CancellationToken ctx)
    {
        Query = Context.Query.TryGetValue("query", out string query) 
            ? query : ""; 

        Page = Context.Query.TryGetValueNumber("page", out int page) 
            ? page : 0; 
            
        Size = Context.Query.TryGetValueNumber("size", out int size) 
            ? size : 0;

        if (Context.Body.Value is SessionParams body)
            _body = body;

        return Task.CompletedTask;
    }

    [RelayCommand]
    public void NavigateToHome()
        => _router.Navigate("home", "My App Home");
}

public record SessionParams(int Id, DateTime Date);

Register Routes

services.AddAvaloniaRouter<ViewModelBasePage>(options =>
{
    options.Register<HomePage,  HomeViewModel>("home");
    options.Register<StorePage, StoreViewModel>("store");
});

MainWindow Integration

public partial class MainWindowViewModel : ViewModelBase
{
    [ObservableProperty] private Control _content;
    [ObservableProperty] private string _title;

    public MainWindowViewModel()
    {
        this.RouterHistory.TitleChanged += (title) =>
        {
            Title = string.IsNullOrWhiteSpace(title)
                ? "Sample Router"
                : $"Sample Router - {title}";
        };

        this.RouterHistory.PageChanged += (page) => Content = page;

        this.RouterHistory.GoTo("home", "Home");
    }
}

XAML:

<StackPanel Grid.Column="1" Margin="10">
    <ContentControl Content="{Binding Content}"/>
</StackPanel>

DryIoc Example

public static IContainer Container { get; set; }

private void ConfigureServices()
{
    var services = new ServiceCollection();

    services.AddAvaloniaRouter<ViewModelBasePage>(options =>
    {
        // base route: myapp://SampleRouterApp/...
        options.Scheme = "myapp"; 
        options.AppName = "SampleRouterApp"; 

        options.HistorySize = 5;

        options.Register<HomePage,  HomeViewModel>("home");
        options.Register<StorePage, StoreViewModel>("store");
        options.SetPage404<Error404Page, Error404ViewModel>();
    });

    var dryIoc = new Container()
        .WithDependencyInjectionAdapter(services)
        .Populate(services);

    Container = dryIoc;
}

Roadmap

  • unify object & query data flow inside NavigationContext
  • optional query-style param helpers
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.