• Stars
    star
    402
  • Rank 107,380 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

In-app live reload for Avalonia applications.

nuget downloads Size License

Live.Avalonia

Live.Avalonia is an experimental project which intends to make the hot reloading feature working in Avalonia-based applications.

In Live.Avalonia, we rely on dotnet watch build .NET Core facility to rebuild an Avalonia project from sources when any of the source files change. Then, we re-embed the updated controls into a simple Avalonia Window. Live.Avalonia could possibly save you a lot of time spent clicking 'Build & Run' in your IDE, or typing dotnet run in the console. Worth noting, that Live.Avalonia doesn't require you to install any particular IDE toolingโ„ข โ€” you can edit files even in Vim, and the app will hot reload ๐Ÿ”ฅ

Getting Started

Warning Live.Avalonia was not extensively tested, and is not guaranteed to work with every project setup, especially if you do some extraordinary stuff with weird MSBuild properties and your output assemblies. Use this tool at your own risk. Thank you for your flexibility.

Important Note By default, dotnet watch build triggers the build only when any .cs file changes. In order to have live reload working for .xaml files too, add the following line to your .csproj file: <Watch Include="**\*.xaml" />. See the Live.Avalonia.Sample project for more info.

Live.Avalonia is distributed via NuGet package manager:

dotnet add package Live.Avalonia

After installing the NuGet package, add the following lines to your App.xaml.cs file:

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

    public override void OnFrameworkInitializationCompleted()
    {
        if (Debugger.IsAttached || IsProduction())
        {
            // Debugging requires pdb loading etc, so we disable live reloading
            // during a test run with an attached debugger.
            var window = new Window();
            window.Content = CreateView(window);
            window.Show();
        }
        else
        {
            // Here, we create a new LiveViewHost, located in the 'Live.Avalonia'
            // namespace, and pass an ILiveView implementation to it. The ILiveView
            // implementation should have a parameterless constructor! Next, we
            // start listening for any changes in the source files. And then, we
            // show the LiveViewHost window. Simple enough, huh?
            var window = new LiveViewHost(this, Console.WriteLine);
            window.StartWatchingSourceFilesForHotReloading();
            window.Show();
        }

        // Here we subscribe to ReactiveUI default exception handler to avoid app
        // termination in case if we do something wrong in our view models. See:
        // https://www.reactiveui.net/docs/handbook/default-exception-handler/
        //
        // In case if you are using another MV* framework, please refer to its 
        // documentation explaining global exception handling.
        RxApp.DefaultExceptionHandler = Observer.Create<Exception>(Console.WriteLine);
        base.OnFrameworkInitializationCompleted();
    }

    // When any of the source files change, a new version of the assembly is 
    // built, and this method gets called. The returned content gets embedded 
    // into the LiveViewHost window.
    public object CreateView(Window window) => new TextBlock { Text = "Hi!" };

    private static bool IsProduction()
    {
#if DEBUG
        return false;
#else
        return true;
#endif
    }
}

Then, run your Avalonia application:

dotnet run

Now, edit the control returned by ILiveView.CreateView, and the app will hot reload! ๐Ÿ”ฅ

Pro tip If you are willing to use an assembly weaving tool like ReactiveUI.Fody for INotifyPropertyChanged injections, extract your view models into a separate assembly. For example, the Live.Avalonia.Sample project references the Live.Avalonia.Sample.Library project in order to have assembly postprocessing working as expected. Beware: if you change the code in the referenced assemblies, the app won't hot reload, and you will have to restart the app on your own to see changes.

Retaining App State

As we discovered in this Twitter thread, the state is retained, if you keep it in your ViewModel and pass it from Window to your View inside the ILiveView.CreateView method. So, if you are willing to keep app state the same after a hot reload, use the following ILiveView.CreateView implementation:

public object CreateView(Window window) {
    if (window.DataContext == null)
        window.DataContext = new AppViewModel();

    // The AppView class will inherit the DataContext
    // of the window. The AppView class can be a 
    // UserControl, a Grid, a TextBlock, whatever.
    return new AppView();
}

Getting Started with F#

Thanks to @AngelMunoz and to @JaggerJo Live.Avalonia now supports MVU and Avalonia.FuncUI as well. See the Live.Avalonia.FuncUI.Sample directory in this repository for a compelling example. The composition root is located inside the Program.fs file.

Important Note By default, dotnet watch build triggers the build only when any .cs file changes. In order to have live reload working for .fs files too, add the following line to your .fsproj file: <Watch Include="**\*.fs" />. See the Live.Avalonia.FuncUI.Sample project for more info.

More Repositories

1

Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
C#
25,320
star
2

AvaloniaEdit

Avalonia-based text editor (port of AvalonEdit)
C#
716
star
3

Avalonia.Samples

Avalonia.Samples aims to provide some minimal samples focusing on a particular issue at a time. This should help getting new users started.
C#
570
star
4

Citrus.Avalonia

Modern styles for Avalonia controls.
C#
542
star
5

avalonia-dotnet-templates

Avalonia Templates for `dotnet new`
C#
478
star
6

AvaloniaVS

Visual Studio Extension for Avalonia
C#
414
star
7

Avalonia.Xaml.Behaviors

Port of Windows UWP Xaml Behaviors for Avalonia Xaml.
C#
376
star
8

Avalonia.Markup.Declarative

Provides helpers for declarative ui in C#
C#
322
star
9

Avalonia.Controls.TreeDataGrid

A combined TreeView/DataGrid for Avalonia.
C#
268
star
10

Avalonia.Labs

Experimental Controls for Avalonia
C#
167
star
11

XamlControlsGallery

C#
159
star
12

AvaloniaMauiHybrid

Brings templated controls, pixel perfect rendering, animations, styling and composition to Maui
C#
152
star
13

AvaloniaUI.QuickGuides

C#
136
star
14

Avalonia.HtmlRenderer

Avalonia front-end for the HtmlRenderer project.
C#
112
star
15

AvaloniaVSCode

Visual Studio Code Extension for Avalonia UI
TypeScript
97
star
16

Avalonia.GIF

GIF image renderer and player for Avalonia UI Framework
C#
91
star
17

Avalonia.NameGenerator

Generates x:Name refs to Avalonia XAML controls.
C#
85
star
18

AvaloniaAutoGrid

C#
78
star
19

Avalonia.Essentials

Essential cross platform APIs for your mobile apps.
C#
65
star
20

Solitaire

C#
64
star
21

Wordle-onia

C#
60
star
22

ControlCatalogStandalone

C#
57
star
23

XamlPlayground

Avalonia Xaml Playground
C#
56
star
24

avalonia-docs

JavaScript
55
star
25

Documentation

AvaloniaUI documentation page source code
53
star
26

Avalonia.VSCodeExtension

This is an experimental VSCode extension. Don't expect it to work.
C#
50
star
27

Avalonia.MusicStore

C#
47
star
28

Avalonia-VSCode-Extension

An awesome extension for Visual Studio Code.
44
star
29

Avalonia.Lottie

Lottie/Bodymovin renderer and player for Avalonia UI Framework.
C#
29
star
30

Avalonia.AxamlAutoCompletion

C#
15
star
31

skiabuild

Makefile
6
star
32

MusicStoreTutorial

C#
6
star
33

Avalonia.TagsAndIdentation

TypeScript
5
star
34

NodeEditor

CSS
4
star
35

Avalonia.WebCam

Experimental cross-platform webcam interface for Avalonia UI Framework.
C#
3
star
36

Avalonia.Toolkit

2
star
37

.github

2
star