Prerequisites

  • Operating system: Windows or macOS
  • IDE: Works with all IDEs, like Visual Studio, Rider, VSCode or any other .NET development tool
  • Project type: any project that supports .NET Standard 2.0

Installation

  • Install the LiveSharp NuGet package into the project where you need the hot-reload functionality. Typically, this would be a .NET Standard project for Xamarin.Forms solution or the main assembly for ASP.NET Core.
  • Install the LiveSharp Server application which can be downloaded from https://github.com/OYIon/LiveSharp/releases

Running the application

Make sure that the LiveSharp Server application is running. Now start the application as you normally do. Switch to the Status panel and you should see something like this:

This means that the application has successfully connected to the server and is now ready to be updated in runtime.

Configuration

By default, every method in your application is available for runtime updates. This might be undesirable in some cases since LiveSharp adds a small overhead to the execution. You can specify what methods to inject by using inject rules. Open livesharp.config file located in the root folder of your project and you should see something like this:

<root>
    <build>
        <start></start>
        <inject>
            <include>*</include>
        </inject>
        <handlers>LiveSharp.XamarinFormsViewHandler,LiveSharp.XamarinFormsViewModelHandler</handlers>
    </build>
    <runtime>
        <pageHotReloadMethod>Build</pageHotReloadMethod>
    </runtime>
</root>

Notice the <inject> node containing * rule. This means that all of the methods in the current project are enabled for LiveSharp. In some cases, you might want to include only certain types. For example, when doing Xamarin Forms UI development these could be types deriving from ContentPage:

<include>Xamarin.Forms.ContentPage</include>

You can also include singular methods:

<include>Xamarin.Forms.ContentPage Build</include>

Or constructors:

<include>MyNamespace.MyType .ctor</include>

If the method has any overloads, you can choose one by providing parameter types:

<include>MyNamespace.MyType MethodName System.Collections.Generic.Dictionary</include>

You can use wildcards:

<include>Xamarin.Forms.Content* Build*</include>

This will match all methods starting with Build that are defined in types derived from types in Xamarin.Forms namespace starting with Content. Like ContentPage or ContentView.

Start rule

LiveSharp needs a starting point to initialize itself. For example, with Xamarin Forms it should be your App type constructor. With Console projects, this should be Program.Main.

Since you might have a different application structure, LiveSharp might not be able to resolve it automatically.

To manually assign the initialization method you can use the following syntax:

<start>MyNamespace.App StartTheApplication</start>

Xamarin.Forms hot-reload

By default LiveSharp expects users to define a Build method for hot-reload to work. If you want to use another method, update runtime section in livesharp.config to look something like this:

<runtime>
    <pageHotReloadMethod>CreateControls</pageHotReloadMethod>
</runtime>

Where CreateControls is the name of your UI building method.

Here is an example of a Page that can be reloaded in runtime:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent(); // You can omit this call if there is no XAML counterpart
        
        Build();
    }
    
    public void Build()
    {
        Content = new StackLayout {
            Children = {
                new Label { Text = "Hello, world!" }
            }
        };
    }
}

Menu