Avalonia Field Guide
About

Module 3 · Lesson 10 of 16

Tokens, scopes, and two themes

Name the role, not the colour. ThemeDictionaries answer for light and dark, and a ThemeVariantScope can answer differently for part of the page.

30 minHeadless-tested

The fastest way to make a desktop app feel wrong is to hardcode #FFFFFF. It looks fine on your machine, then someone opens it at night and the window is a flashlight.

src/CourseApp/Theme/Tokens.axaml
<ResourceDictionary xmlns="https://github.com/avaloniaui"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!--
        Design tokens. These are the only colors the app uses; every view refers to
        them by key so the light and dark variants stay in step.
    -->
    <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Light">
            <SolidColorBrush x:Key="PageBackground" Color="#FBFAF7" />
            <SolidColorBrush x:Key="Surface" Color="#FFFFFF" />
            <SolidColorBrush x:Key="SurfaceMuted" Color="#F3EFE8" />
            <SolidColorBrush x:Key="Ink" Color="#1A1815" />
            <SolidColorBrush x:Key="InkMuted" Color="#6B655C" />
            <SolidColorBrush x:Key="Rule" Color="#E4DDD1" />
            <SolidColorBrush x:Key="Accent" Color="#A8381B" />
            <SolidColorBrush x:Key="AccentSoft" Color="#F4E2D9" />
            <SolidColorBrush x:Key="Ok" Color="#2F6B4F" />
        </ResourceDictionary>
        <ResourceDictionary x:Key="Dark">
            <SolidColorBrush x:Key="PageBackground" Color="#121110" />
            <SolidColorBrush x:Key="Surface" Color="#1A1815" />
            <SolidColorBrush x:Key="SurfaceMuted" Color="#23201C" />
            <SolidColorBrush x:Key="Ink" Color="#ECE5D8" />
            <SolidColorBrush x:Key="InkMuted" Color="#A39A8D" />
            <SolidColorBrush x:Key="Rule" Color="#2C2822" />
            <SolidColorBrush x:Key="Accent" Color="#E07A54" />
            <SolidColorBrush x:Key="AccentSoft" Color="#3A231A" />
            <SolidColorBrush x:Key="Ok" Color="#7BBB96" />
        </ResourceDictionary>
    </ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
Screenshot of the themes lesson sample
The lesson's sample, rendered headlessly at 1280×800. Light and dark are both captured.

One dictionary per variant

ThemeDictionaries is keyed by theme variant. When the app's RequestedThemeVariant is Light, a lookup for Surface finds the light brush; Dark finds the other one. The keys are identical, which is what makes the rest of the app theme-agnostic.

Two rules keep this maintainable:

  1. Name roles, not colours. PageBackground, Surface, Rule, Accent — not LightGray or Orange.
  2. Never reference a token's value directly. Surface is a SolidColorBrush today and could be a LinearGradientBrush tomorrow; code that assumed a Color will break.

DynamicResource vs StaticResource

  • {StaticResource Surface} resolves once, at load time. Cheap, and wrong for anything that must react to a theme change.
  • {DynamicResource Surface} subscribes. When the variant changes, the property updates.

If a control keeps its light colour after you flip the theme, this is almost always why. The exception is a resource that genuinely never changes — a corner radius, a font size.

A local scope

src/CourseApp/Lessons/09_Themes/ThemeView.axaml:8–30
            <shell:PageHeader
                Kicker="Lesson 09 · Themes"
                Title="Tokens, scopes, and two themes for free"
                Blurb="Nothing in these panels names a colour. They ask for Surface, Ink, and Accent; a ThemeVariantScope decides which dictionary answers. The same page renders in either theme without a single if statement." />

            <Grid ColumnDefinitions="*,*" ColumnSpacing="16">
                <Border Classes="card" Padding="0">
                    <StackPanel>
                        <TextBlock Text="LIGHT SCOPE" Classes="kicker" Margin="20,18,20,0" Foreground="{DynamicResource InkMuted}" />
                        <ThemeVariantScope x:Name="LightScope" RequestedThemeVariant="Light">
                            <Border Background="{DynamicResource PageBackground}" Padding="20" Margin="0,12,0,0">
                                <StackPanel Spacing="10">
                                    <Border Background="{DynamicResource Surface}" BorderBrush="{DynamicResource Rule}" BorderThickness="1" CornerRadius="4" Padding="12">
                                        <TextBlock Text="Surface on PageBackground" Foreground="{DynamicResource Ink}" />
                                    </Border>
                                    <Border Background="{DynamicResource SurfaceMuted}" CornerRadius="4" Padding="12">
                                        <TextBlock Text="SurfaceMuted" Foreground="{DynamicResource InkMuted}" />
                                    </Border>
                                    <Border Background="{DynamicResource Accent}" CornerRadius="4" Padding="12">
                                        <TextBlock Text="Accent" Foreground="#FFFFFF" />
                                    </Border>
                                </StackPanel>
                            </Border>

ThemeVariantScope overrides the variant for its subtree. The sample uses it to show both variants on one page, which is also the easiest way to review a palette: put them side by side, photograph it, and look for contrast failures.

In a real app the toggle is global:

Application.Current!.RequestedThemeVariant =
    Application.Current.ActualThemeVariant == ThemeVariant.Dark
        ? ThemeVariant.Light
        : ThemeVariant.Dark;

Persist the choice, and default to ThemeVariant.Default so the operating system decides on first run.

The theme is not the design system

Fluent2Theme supplies control templates and a large set of built-in brushes. Your tokens sit on top and describe your product. Do not fight the theme inside a control template; define your own ControlTheme (next lesson) and let it win.

Check contrast, not vibes. Screenshots lie about contrast in dark mode. Run a computed contrast check on your token pairs — body text and muted text against both surfaces — before shipping.

Try it

  1. Add a Warning token to both dictionaries and use it for one pill.
  2. Add a ThemeVariantScope with Default inside the dark panel and watch it follow the application again.
  3. Delete the light dictionary entry for Accent and see what the lookup falls back to. Then put it back.

Next: a control with a contract.

Files this lesson draws from

  • src/CourseApp/Theme/Tokens.axaml
  • src/CourseApp/Theme/Styles.axaml
  • src/CourseApp/Lessons/09_Themes/ThemeView.axaml

VerifiedThe verifier hosts the page and asserts that the scoped panels resolve Dark and Light independently on the same screen.

Back to top