15a932027f | ||
---|---|---|
Avalonia.HotReload | ||
Avalonia.HotReload.Sample | ||
.gitignore | ||
Avalonia.HotReload.sln | ||
LICENSE | ||
README.md | ||
demo.gif |
README.md
Avalonia.HotReload
This repository demonstrates how to use the hot reload feature in your Avalonia applications. The core idea is to rely on dotnet watch
for rebuilding the projects from sources when any of the source files change, and to re-embed the updated controls into the Window
without any need to press the 'Run' button by hands multiple times.
Getting Started
- Create a new project based on a template from the Avalonia Templates repository. Or, use AvaloniaVS.
- Install the
Avalonia.ReactiveUI
package into your newly created project:
# Execute this command from the project root.
dotnet add package Avalonia.ReactiveUI
- Copy-paste the
AvaloniaReloadingWindow.cs
file into your newly created project. - Create a static method
CreateReloadableControl
in your theProgram.cs
file:
// This method will be the hot-reloadable composition root of your Avalonia application.
// Remember to use this signature! Otherwise the things won't work.
public static object CreateReloadableControl(Window window) => new TextBlock { Text = "Ok" };
- Instantiate the
AvaloniaReloadingWindow
in yourApp.xaml.cs
file as such:
// Obtain the assembly of the project by doing typeof on any type from the assembly.
// Then, instantiate the reloading Window class by passing the current project
// assembly to it, as well as the logger. Then, show the window. Using multiple
// reloading windows isn't currently supported.
var assembly = typeof(App).Assembly;
var window = new AvaloniaReloadingWindow(assembly, Console.WriteLine);
window.Show();
- Run your project using .NET CLI, as follows:
# Don't use the 'dotnet run' without the '--no-build' argument.
dotnet run --no-build
Important Note: Don't use the
dotnet run
command without the--no-build
argument! Always usedotnet run --no-build
, ordotnet build && dotnet run --no-build
. Otherwise the executable file will get locked, see: https://github.com/dotnet/sdk/issues/11766
- Done! Make some changes in the control returned by
CreateReloadableControl
, pressCtrl+S
and the app will hot-reload. If you experience any issues with this setup, try cloning this repository and running theAvalonia.HotReload.Sample
project by executingdotnet run --no-build
from the project root.
Important Note: By default,
dotnet watch
tracks changes in.cs
files only. In order to have hot-reload working with.xaml
files, add the<Watch Include="**\*.xaml" />
directive to your.csproj
file. See the project file in the demo project for more context.