NavigationFrame.Avalonia 1.6.0

There is a newer version of this package available.
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
                    
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="NavigationFrame.Avalonia" Version="1.6.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NavigationFrame.Avalonia" Version="1.6.0" />
                    
Directory.Packages.props
<PackageReference Include="NavigationFrame.Avalonia" />
                    
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 NavigationFrame.Avalonia --version 1.6.0
                    
#r "nuget: NavigationFrame.Avalonia, 1.6.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 NavigationFrame.Avalonia@1.6.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=NavigationFrame.Avalonia&version=1.6.0
                    
Install as a Cake Addin
#tool nuget:?package=NavigationFrame.Avalonia&version=1.6.0
                    
Install as a Cake Tool

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

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.

  1. 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
    }
}
  1. 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 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.