NavigationFrame.Avalonia
1.6.0
See the version list below for details.
dotnet add package NavigationFrame.Avalonia --version 1.6.0
NuGet\Install-Package NavigationFrame.Avalonia -Version 1.6.0
<PackageReference Include="NavigationFrame.Avalonia" Version="1.6.0" />
<PackageVersion Include="NavigationFrame.Avalonia" Version="1.6.0" />
<PackageReference Include="NavigationFrame.Avalonia" />
paket add NavigationFrame.Avalonia --version 1.6.0
#r "nuget: NavigationFrame.Avalonia, 1.6.0"
#:package NavigationFrame.Avalonia@1.6.0
#addin nuget:?package=NavigationFrame.Avalonia&version=1.6.0
#tool nuget:?package=NavigationFrame.Avalonia&version=1.6.0
NavigationFrame.Avalonia
A flexible and powerful navigation framework for Avalonia UI that supports Layouts (similar to Blazor/ASP.NET Core), built-in Transitions, and MVVM-friendly lifecycle events.
Features
- Layout Support: Define common layouts (e.g., Master-Detail, Sidebar) and wrap your pages automatically.
- Navigation History: Built-in support for Back and Forward navigation stacks.
- Caching: Configurable page caching to preserve state.
- Transitions: A wide variety of built-in animations (Slide, DrillIn, Entrance, Zoom).
- MVVM Friendly: Lifecycle interfaces for both Views (
IPageControl) and ViewModels (IPageContext). - Async Navigation: Fully asynchronous navigation pipeline.
Getting Started
1. Add Resources
You must add the framework's resources to your App.axaml to ensure controls render correctly.
<Application ...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceInclude Source="avares://NavigationFrame.Avalonia/Styling/Resources.axaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
2. Implement IViewFactory
You need to tell the framework how to resolve your Views (Controls).
using NavigationFrame.Avalonia;
using Avalonia.Controls;
public class ViewFactory : IViewFactory
{
public Control ResolveView(object key)
{
if (key is Type type)
{
var control = (Control)Activator.CreateInstance(type)!;
// Ensure DataContext is set!
control.DataContext = CreateViewModelForView(type);
return control;
}
// Handle other key types (e.g., strings) or dependency injection here
throw new ArgumentException($"Cannot resolve view for key {key}");
}
private object CreateViewModelForView(Type viewType)
{
// Use your DI container or create instance manually
// e.g. return _serviceProvider.GetService(ViewModelTypeMap[viewType]);
throw new NotImplementedException();
}
}
3. Setup LayoutAwareFrame
Add the LayoutAwareFrame to your main window or view.
<Window xmlns:nav="using:NavigationFrame.Avalonia" ...>
<nav:LayoutAwareFrame x:Name="AppFrame" />
</Window>
In your code-behind (or setup logic), initialize the dependencies:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 1. Setup Navigation Service
var navService = new DefaultNavigationService();
// 2. Setup View Factory
var viewFactory = new ViewFactory(); // Your implementation
// 3. Configure Frame
navService.SetNavigator(AppFrame); // Link service to frame
AppFrame.ViewFactory = viewFactory;
// 4. Navigate to Home
navService.NavigateAsync(new NavigationOptions(typeof(HomePage)));
}
}
Usage
Navigation
Inject INavigator into your ViewModels.
// Simple navigation
await _navigator.NavigateAsync(new NavigationOptions(typeof(DetailsPage)));
// Navigation with Parameter
await _navigator.NavigateAsync(new NavigationOptions(typeof(DetailsPage), "MyParameter"));
// Navigation with Transition
await _navigator.NavigateAsync(new NavigationOptions(typeof(DetailsPage))
{
TransitionInfo = new SlideTransitionInfo { Effect = SlideTransitionEffect.FromRight }
});
// Or use predefined helpers
await _navigator.NavigateAsync(NavigationOptions.SlideIn());
// Go Back
await _navigator.GoBackAsync();
Lifecycle Events
For ViewModels (IPageContext)
Implement IPageContext in your ViewModel to handle navigation events.
public class MyPageViewModel : IPageContext
{
private WeakReference<IPageHost>? _host;
// Accessed via property
public INavigator NavigationService => _host?.TryGetTarget(out var host) == true ? host.Navigator : throw new Exception("Host released");
public void OnInit(WeakReference<IPageHost> host)
{
_host = host;
}
public string Title => "My Page";
// Load data here (already in background thread)
public async Task OnNavigatingTo(NavigationArgs args)
{
if (args.Parameter is string id)
{
await LoadDataAsync(id);
}
}
public Task<bool> OnNavigatingAway(NavigationArgs args)
{
// Return true to cancel navigation (e.g., unsaved changes)
return Task.FromResult(false);
}
public Task OnNavigatedAway(NavigationArgs args)
{
// Indicates that the page is no longer visible.
return Task.CompletedTask;
}
public void OnPageReleased()
{
// Final cleanup when removed from cache/stack
}
}
For Views (IPageControl)
Implement IPageControl in your UserControl code-behind if you need UI-specific lifecycle events.
Layouts
Layouts allow you to define a common shell (like a header or sidebar) for multiple pages.
- Create a Layout Control: Implement
ILayoutControl.
public partial class MainLayout : UserControl, ILayoutControl
{
public Control GetBodyHolder()
{
// Return the container where the page content should be placed
return this.FindControl<ContentPresenter>("BodyArea");
}
public void OnBodyChanged(Control currentBody)
{
// Optional: React to body change
}
}
Assign Layout to Page:
Use the
[Layout]attribute on your View (UserControl) class.[Layout(typeof(MainLayout))] public partial class HomePage : UserControl { public HomePage() { InitializeComponent(); } }
Key Components
- LayoutAwareFrame: The core control handling navigation, history, and layouts.
- INavigator: The interface used to request navigation.
- IViewFactory: The bridge between navigation keys (Types) and actual UI Controls.
- IPageContext: ViewModel interface for navigation lifecycle.
- ILayoutControl: Interface for creating Layout shells.
Contact
For questions or support, please reach out to richardplus@foxmail.com
| 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 (>= 11.3.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 2.0.7 | 150 | 12/31/2025 | |
| 2.0.6 | 144 | 12/31/2025 | |
| 2.0.5 | 145 | 12/30/2025 | |
| 2.0.4 | 141 | 12/29/2025 | |
| 2.0.3 | 144 | 12/28/2025 | |
| 2.0.2 | 142 | 12/28/2025 | |
| 2.0.0 | 159 | 12/27/2025 | |
| 1.9.9 | 231 | 12/24/2025 | |
| 1.9.8.1 | 216 | 12/22/2025 | |
| 1.9.8 | 293 | 12/22/2025 | |
| 1.9.6 | 176 | 12/20/2025 | |
| 1.9.0 | 267 | 12/14/2025 | |
| 1.7.0 | 261 | 12/12/2025 | |
| 1.6.0 | 557 | 12/10/2025 | |
| 1.5.1 | 472 | 12/8/2025 | |
| 1.5.0 | 466 | 12/8/2025 | |
| 1.4.0 | 585 | 12/7/2025 |