Avalonia Field Guide
About

Module 1 · Lesson 01 of 16

One window, three files

Program starts the app, App owns the lifetime, MainWindow is a control tree. Everything else is built on these three.

20 minHeadless-tested

Every Avalonia application, from a 200-line utility to a full IDE, begins with the same three things: an entry point, an Application object, and a window.

Screenshot of the anatomy lesson sample
The lesson's sample, rendered headlessly at 1280×800. Light and dark are both captured.

Program.cs is just a builder

src/CourseApp/Program.cs
using Avalonia;
using System;

namespace CourseApp;

internal static class Program
{
    [STAThread]
    public static void Main(string[] args) =>
        BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);

    public static AppBuilder BuildAvaloniaApp() =>
        AppBuilder.Configure<App>()
            .UsePlatformDetect()
            .WithInterFont()
            .LogToTrace();
}

BuildAvaloniaApp() returns an AppBuilder. Nothing has happened yet — no window, no rendering, no platform. StartWithClassicDesktopLifetime is what commits: it creates the platform, runs App.Initialize(), and then keeps a message loop alive on the calling thread for as long as windows exist.

That loop is the single most important fact about desktop development: one thread owns the UI, and everything you do to a control has to happen on it. We will come back to this in lesson 6.

[STAThread] is a Windows COM requirement. On Linux it costs nothing and keeps the same source file portable, which is why the template keeps it.

Common mistake. Doing work before BuildAvaloniaApp(). Static constructors that touch brushes, fonts, or Application.Current will run before the platform exists and fail in ways that look like missing resources.

App is the application, not the window

src/CourseApp/App.axaml
<Application xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="CourseApp.App"
             RequestedThemeVariant="Default">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceInclude Source="avares://CourseApp/Theme/Tokens.axaml" />
                <ResourceInclude Source="avares://CourseApp/Theme/Controls.axaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

    <Application.Styles>
        <Fluent2Theme />
        <StyleInclude Source="avares://CourseApp/Theme/Styles.axaml" />
    </Application.Styles>
</Application>

Two jobs live here: resources (the colour tokens and control themes used by every view) and styles (the Fluent2 theme plus this project's selectors). Both are application-scoped, so a window that is created later can still resolve them.

Fluent2Theme is Avalonia 12's theme package. In older tutorials you will see FluentTheme; the package and element name changed, and mixing them produces a design-time error rather than a silent fallback.

src/CourseApp/App.axaml.cs
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using CourseApp.Shell;

namespace CourseApp;

public partial class App : Application
{
    public override void Initialize() => AvaloniaXamlLoader.Load(this);

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            desktop.MainWindow = new MainWindow
            {
                DataContext = new MainViewModel(),
            };
        }

        base.OnFrameworkInitializationCompleted();
    }
}

OnFrameworkInitializationCompleted is the one place that knows which lifetime is running. Desktop assigns desktop.MainWindow. A mobile or browser host would do something else here — that is the entire reason the lifetime abstraction exists.

The view model is the app

src/CourseApp/Lessons/01_Anatomy/CounterViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace CourseApp.Lessons.Anatomy;

public partial class CounterViewModel : ObservableObject
{
    public string Greeting => "Hello, Avalonia";

    [ObservableProperty]
    private int _clicks;

    [RelayCommand]
    private void Press() => Clicks++;
}

This is a plain class. It has no reference to Avalonia, no window, no control. The [ObservableProperty] attribute generates a Clicks property that raises PropertyChanged; [RelayCommand] generates an ICommand whose Execute calls Press. The XAML binds to those generated members.

If you can test it without opening a window, you have written a view model. If you cannot, you have written a control.

Try it

  1. Change Greeting to include the click count and see the page update without touching the XAML.
  2. Add a [RelayCommand] void Reset() that sets Clicks = 0, and a second button bound to it.
  3. Run dotnet run --project src/CourseApp.Verify after the change. The verifier re-renders this page; if the binding broke, you will see it in the PNG.

Next: bindings the compiler can see.

Files this lesson draws from

  • src/CourseApp/Program.cs
  • src/CourseApp/App.axaml
  • src/CourseApp/Lessons/01_Anatomy/CounterViewModel.cs

VerifiedThe verifier renders this page headlessly at 1280×800 in both themes and asserts that the generated command increments the view model twice.

Back to top