Hot reload
Hot reload applies edits to .axaml and .cs files in a running Avalonia application without restarting it. The AvaloniaUI.DiagnosticsSupport.HotReload package plugs into the .NET Hot Reload pipeline: when you save a file, matching controls, styles, resources and data templates are rebuilt in place.
What hot reload updates
With the hot reload package, these edits are applied live to your running application:
| Change | Behavior |
|---|---|
Controls (files with x:Class) | Existing instances in the visual tree are rebuilt in place, keeping their positions where possible. |
| Styles | Application-level and control-level styles are reapplied. Selector and setter changes take effect at once. |
| Resource dictionaries | Merged dictionaries are reloaded and dependents are refreshed. |
| Data templates | Templates are regenerated and controls bound to them are refreshed. |
{StaticResource} references | Rewritten to {DynamicResource} during hot reload, so resource edits propagate without a restart. |
| Some common uses of hot reload are: |
- Resource brushes and values. Change a color, brush, or font size in a resource dictionary and every control that references it updates. Both
{DynamicResource}and{StaticResource}references pick up the change, because static references are rewritten during hot reload. - Data templates. Change the icon, colors, spacing, or layout of a data template, and every control that uses it, including list items and content presenters, rebuilds with the new template.
- Control markup. Adjust the layout, add or remove elements, or edit text in a view (a file with
x:Class). The live instance is rebuilt in place. - Styles. Edit a selector or setter in an application-level or control-level style, and the new styling is re-applied at once.
- Code-behind. Change an event handler or method body in an
.axaml.csfile. C# edits apply under the standard .NET Hot Reload rules.
Prerequisites
Before you begin, make sure you have:
- Avalonia 12.0 or newer.
- A valid Avalonia license key that includes access to
AvaloniaUI.DiagnosticsSupport.HotReload. You can get a key from the Avalonia customer portal. The same key may cover other licensed Avalonia packages, such asChartsorTreeDataGrid. - A hot reload driver. Either the
dotnet watchcommand or an IDE that supports .NET Hot Reload (such as Visual Studio). See Step 3.
Getting started
-
Install the
AvaloniaUI.DiagnosticsSupport.HotReloadNuGet package by runningdotnet add package. -
To keep hot reload out of release builds, go to your
.csprojfile and wrap the<PackageReference>for the hot reload package in aDebugcondition. This ensures it never ships. -
Include your Avalonia license key in the executable project file (
.csproj). Your license key is available from the Avalonia portal.
Running with hot reload
Start your application through a tool that supports .NET Hot Reload.
- dotnet watch
- Visual Studio
- VS Code
- Rider
Run dotnet watch on the platform head project. It rebuilds and applies changes when you save.
dotnet watch --project YourApp.Desktop
This is the most reliable driver and works the same across every editor and platform.
On mobile platforms, dotnet watch requires .NET 11 or newer.
Start the app with the debugger (F5). After each edit, use Apply Code Changes (the hot reload button) on the toolbar, or turn on Hot Reload on File Save from the button's dropdown menu.
Install the C# Dev Kit extension, which brings .NET Hot Reload to VS Code.
Then, you can either:
- Run the app with
dotnet watchfrom the integrated terminal, or - Start a debug session (F5) with hot reload applying when you save a file.
Although JetBrains Rider has a hot reload feature, it does not drive .NET metadata updates. Instead, you can either:
- Run the app with
dotnet watchfrom Rider's terminal, or - Enable the file-system watcher and start the app normally.
Enabling the file-system watcher
Avalonia hot reload can still pick up .axaml edits with a built-in file-system watcher, even if .NET Hot Reload is not attached. This can occur if you run the app outside dotnet watch, or if you are using Rider.
To enable the file-system watcher, add an MSBuild property in your .csproj:
<PropertyGroup>
<AvaloniaHotReloadEnableFileWatcher>true</AvaloniaHotReloadEnableFileWatcher>
</PropertyGroup>
The file-system watcher can hot-reload .axaml files without .NET Hot Reload, but not .cs files. If you need hot reload for your C# code-behind, pair the watcher with dotnet watch.
Verifying hot reload
With the app running:
- Open an
.axamlfile. Change a property, for example, aBackgroundcolor or some text. Save the file. - Watch the running window. It should update without losing its current view.
- Open the matching
.axaml.csfile. Adjust an event handler. Save the file. - Trigger the event.
- In the running window, confirm that the adjusted event reflects your edit. Diagnostic output is written to the trace log under the
HotReloadcategory.
C# changes follow the standard .NET Hot Reload rules.
Initializing manually
The auto-setup method described above covers most usages of hot reload. If you need a custom lifecycle, multiple Application instances, or deferred startup, you can instead call the initializer manually:
using AvaloniaUI.DiagnosticsSupport.HotReload;
HotReloadExtensions.InitializeHotReload(
Application.Current!,
enableFileWatcher: true,
rewriteStaticResources: true);
The engine initializes once per process, so later calls have no effect. To surface hot reload activity in your logging, subscribe to HotReloadDiagnostics.EntryLogged.
Limitations
- WebAssembly is not supported. Hot reload works on desktop and mobile platforms only.
- C# edits follow the normal .NET Hot Reload rules. Adding fields or changing method signatures counts as a rude edit and needs a restart.
- Controls are rebuilt rather than mutated, so non-XAML states are reset when a control reloads.