Avalonia Field Guide
About

Module 2 · Lesson 08 of 16

Two screens, one ContentControl

Navigation is a view model swap. One host control, a current page, and a back command — no new windows.

25 minHeadless-tested

A desktop app is not a set of windows. It is one window whose content changes. Opening a second window for every screen means duplicate chrome, duplicate state, and a window manager full of clutter.

src/CourseApp/Lessons/08_Navigation/ProjectsViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace CourseApp.Lessons.Navigation;

public sealed record Project(string Name, string Summary, string Detail);

public partial class ProjectsViewModel : ObservableObject
{
    public IReadOnlyList<Project> Projects { get; } =
    [
        new("Field Guide", "The site you are reading.", "Sixteen lessons rendered from one sample app, verified on every build."),
        new("Desk Clock", "A tiny always-on-top timer.", "Starts a window, keeps state in a view model, and survives a restart."),
        new("Log Tail", "Follow a file that keeps growing.", "Streams lines into a virtualized list and never blocks the UI thread."),
    ];

    [ObservableProperty]
    private Project? _selected;

    [ObservableProperty]
    private Project? _current;

    [ObservableProperty]
    private bool _isDetail;

    [RelayCommand(CanExecute = nameof(CanOpen))]
    private void Open()
    {
        Current = Selected;
        IsDetail = true;
    }

    [RelayCommand]
    private void Back()
    {
        IsDetail = false;
        Current = null;
    }

    bool CanOpen() => Selected is not null;

    partial void OnSelectedChanged(Project? value) => OpenCommand.NotifyCanExecuteChanged();
}
Screenshot of the navigation lesson sample
The lesson's sample, rendered headlessly at 1280×800. Light and dark are both captured.

The mechanism

The sample keeps two booleans and a current item:

[ObservableProperty] private Project? _current;
[ObservableProperty] private bool _isDetail;

The view binds IsVisible to the inverse:

<Border IsVisible="{Binding !IsDetail}"> … list … </Border>
<Border IsVisible="{Binding IsDetail}"> … detail … </Border>

Back sets IsDetail = false and clears Current. That is a navigation stack of depth one, which is all most apps need. For a real stack, keep a Stack<PageState> and push/pop.

Avalonia 12 ships a Page navigation system. Page, ContentPage, and a navigation host are available if you want framework-managed transitions and lifecycle events. Learn the primitive first: when the built-in system surprises you, you will know exactly which object it is swapping.

The trap: state on the page

A detail page that reads a global singleton "current project" breaks the moment a second tab or a second window opens. Pass what the page needs through its view model, and keep the collection in one service. The sample hides that problem by owning everything in one class; a real app should not.

Transitions without ceremony

Swapping content is instantaneous, which can feel like a glitch. A TransitioningContentControl or a Transitions collection on the container is enough to fade the new page in. Keep it under 150 ms; navigation is not an animation showcase.

Try it

  1. Replace the two borders with a ContentControl bound to a PageViewModel property, and use a DataTemplate per page type. This is the ViewLocator pattern, and it scales past two screens.
  2. Add a third page (settings) and a command that opens it from the list.
  3. Add CanGoBack so the back button is disabled at the root.

Next: tokens, scopes, and two themes.

Files this lesson draws from

  • src/CourseApp/Lessons/08_Navigation/ProjectsViewModel.cs
  • src/CourseApp/Lessons/08_Navigation/ProjectsView.axaml

VerifiedThe verifier asserts Open is disabled with no selection, opens a project and checks the detail page is current, then goes back and checks the list is restored.

Back to top