Skip to content

NeverMorewd/AsyncNavigation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

128 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AsyncNavigation

A lightweight async navigation framework for .NET desktop apps, built on Microsoft.Extensions.DependencyInjection.

CI NuGet NuGet NuGet License: MIT .NET

中文文档 · Live Demo


Features

Async/Await Native Navigation is fully async end-to-end with CancellationToken support
DI-First Views and view models are resolved from the DI container
Navigation Guard Block navigation away with INavigationGuard (e.g. unsaved changes)
Interceptors Run cross-cutting logic (auth, analytics) via INavigationInterceptor
Dialog Service Async dialog and window management built-in
Multiple Region Types ContentControl, ItemsControl, and TabControl regions
History Navigation GoForwardAsync / GoBackAsync out of the box
Lifecycle Management Automatic view caching, eviction, and disposal — no memory leaks
Native AOT Full Avalonia AOT / trimming support, zero extra config
Framework-Agnostic Works with any MVVM framework
Minimal Deps Only Microsoft.Extensions.DependencyInjection.Abstractions >= 8.0

Installation

# Avalonia
dotnet add package AsyncNavigation.Avalonia

# WPF
dotnet add package AsyncNavigation.Wpf

Quick Start

1. Register services

services.AddNavigationSupport()
        .RegisterView<HomeView, HomeViewModel>("Home")
        .RegisterView<SettingsView, SettingsViewModel>("Settings")
        .RegisterDialog<ConfirmView, ConfirmViewModel>("Confirm");

2. Declare a region in XAML

xmlns:an="https://github.com/NeverMorewd/AsyncNavigation"

<ContentControl an:RegionManager.RegionName="MainRegion" />

3. Navigate

// Navigate
await _regionManager.RequestNavigateAsync("MainRegion", "Home");

// History
await _regionManager.GoBackAsync("MainRegion");
await _regionManager.GoForwardAsync("MainRegion");

// Dialog
var result = await _dialogService.ShowViewDialogAsync("Confirm");

4. React to navigation in view models

// Derive from NavigationAwareBase and override only what you need.
public class HomeViewModel : NavigationAwareBase
{
    public override async Task OnNavigatedToAsync(NavigationContext context)
    {
        await LoadDataAsync(context.CancellationToken);
    }
}

Navigation Guard

public class EditViewModel : NavigationAwareBase, INavigationGuard
{
    public async Task<bool> CanNavigateAsync(NavigationContext context, CancellationToken ct)
    {
        // Return false to cancel — show a confirmation dialog here if needed.
        return !HasUnsavedChanges;
    }
}

Interceptors

public class AuthInterceptor : INavigationInterceptor
{
    public Task OnNavigatingAsync(NavigationContext context)
    {
        if (!_auth.IsLoggedIn)
            throw new OperationCanceledException("Not authenticated.");
        return Task.CompletedTask;
    }

    public Task OnNavigatedAsync(NavigationContext context) => Task.CompletedTask;
}

// Register
services.AddNavigationSupport()
        .RegisterNavigationInterceptor<AuthInterceptor>();

License

MIT

About

A lightweight asynchronous navigation framework based on Microsoft.Extensions.DependencyInjection

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages