Get Started on VS Code

This guide will walk you through the set-up process for building apps with Uno under Windows, Linux or macOS.

See these sections for information about using Uno Platform with:

Prerequisites

Verify your developer environment

  1. Open a command-line prompt, Windows Terminal if you have it installed, or else Command Prompt or Windows Powershell from the Start menu.

  2. a. Install the tool by running the following command from the command prompt:

    dotnet tool install -g uno.check
    

    b. To update the tool, if you already have an existing one:

    dotnet tool update -g uno.check
    
  3. Run the tool from the command prompt with the following command:

    uno-check
    
  4. Follow the instructions indicated by the tool

Note

When using a Visual Studio Preview version, you will need to run uno-check --pre.

You can find additional information about uno-check here.

Developing an Uno Platform project

Create the project

Let's start by updating the Uno project templates. In a terminal, type the following command:

dotnet new install Uno.Templates

Then inside the same terminal, type the following to create a new project:

dotnet new unoapp -o MyApp -preset=blank -platforms android ios maccatalyst wasm gtk --vscode
Tip

MyApp is the name you want to give to your project.

This will create a blank template app that only contains the WebAssembly, Skia+GTK and Mobile platforms support.

Important

Mobile targets cannot be built under Linux. In such operating system, you'll need to remove android ios maccatalyst from the platforms list.

Prepare the application

  1. Open the project using Visual Studio Code. In the terminal type

    code ./MyApp
    

    For this command to work you need to previously have configured Visual Studio Code to be launched from the terminal.

  2. Visual Studio Code will ask to restore the NuGet packages.

  3. Once the project has been loaded, in the status bar at the bottom left of VS Code, MyApp.sln is selected by default. Select MyApp.Wasm.csproj, MyApp.Skia.Gtk.csproj or MyApp.Mobile.csproj instead.

Modify the template

  1. In MainPage.xaml, replace the Grid's content with the following:

    <StackPanel>
        <TextBlock x:Name="txt"
                    Text="Hello, world!"
                    Margin="20"
                    FontSize="30" />
        <Button Content="click"
                Click="{x:Bind OnClick}" />
    </StackPanel>
    
  2. In your MainPage.xaml.cs, add the following method:

    private void OnClick()
    {
        var dt = DateTime.Now.ToString();
        txt.Text = dt;
    }
    

Run and Debug application

WebAssembly

  1. In the debugger section of the Code activity bar
    • Select Debug (Chrome, WebAssembly)
    • If needed, the project from MyApp.sln to MyApp.Wasm.csproj
  2. Press F5 to start the debugging session
  3. Place a breakpoint inside the OnClick method
  4. Click the button in the app, and the breakpoint will hit

Skia GTK

  1. In the debugger section of the Code activity bar
    1. Select Skia.GTK (Debug)
    2. If needed, the project from MyApp.sln to MyApp.Skia.Gtk.csproj
  2. Press F5 to start the debugging session
  3. Place a breakpoint inside the OnClick method
  4. Click the button in the app, and the breakpoint will hit

Note that C# Hot Reload is not available when running with the debugger. In order to use C# Hot Reload, run the app using the following:

  • On Windows, type the following:

    $env:DOTNET_MODIFIABLE_ASSEMBLIES="debug"
    dotnet run
    
  • On Linux or macOS:

    export DOTNET_MODIFIABLE_ASSEMBLIES=debug
    dotnet run
    

Mobile Targets (iOS, Android, Mac Catalyst)

The Uno Platform extension provides support for debugging:

  • The Android target on Windows
  • The iOS, Android and Mac Catalyst targets on macOS

It is also possible to use Remote - SSH addin to connect to a macOS machine from a Windows or Linux machine to debug iOS and Mac Catalyst apps remotely.

Debugging for Android

  • In the status bar, select the MyApp.Mobile project (by default MyApp.sln is selected)

    mobile project name

  • To the right of MyApp.Mobile, click on the target framework to select net7.0-android | Debug

    android target framework

  • Then, to the right of the target framework, select the device to debug with. You will need to connect an android device, or create an Android simulator.

    android device name

  • Finally, in the debugger side menu, select the Uno Plaform Mobile profile

  • Either press F5 or press the green arrow to start the debugging session.

Once your app is running, place a breakpoint in the OnClick method, the breakpoint will be hit when clicking the button in the app.

You can find advanced Code debugging topic here.

Using code snippets

Adding a new Page

  1. In the MyApp folder, create a new file named Page2.xaml
  2. Type page then press the tab key to add the page markup
  3. Adjust the name and namespaces as needed
  4. In the MyApp folder, create a new file named Page2.xaml.cs
  5. Type page then press the tab key to add the page code behind C#
  6. Adjust the name and namespaces as needed

Adding a new UserControl

  1. In the MyApp folder, create a new file named UserControl1.xaml
  2. Type usercontrol then press the tab key to add the page markup
  3. Adjust the name and namespaces as needed
  4. In the MyApp folder, create a new file named UserControl1.xaml.cs
  5. Type usercontrol then press the tab key to add the page code behind C#
  6. Adjust the name and namespaces as needed

Adding a new ResourceDictionary

  1. In the MyApp folder, create a new file named ResourceDictionary1.xaml
  2. Type resourcedict then press the tab key to add the page markup

Other snippets

  • rd creates a new RowDefinition
  • cd creates a new ColumnDefinition
  • tag creates a new XAML tag
  • set creates a new Style setter
  • ctag creates a new TextBlock close XAML tag

Updating an existing application to work with VS Code

An existing application needs additional changes to be debugged properly.

  1. At the root of the workspace, create a folder named .vscode
  2. Inside this folder, create a file named launch.json and copy the contents of this file.
  3. Replace all instances of UnoQuickStart with your application's name in launch.json.
  4. Inside this folder, create a file named tasks.json and copy the contents of this file.

Known limitations for VS Code support

  • C# Debugging is not supported when running in a remote Linux Container, Code Spaces or GitPod.
  • C# Hot Reload for WebAssembly only supports modifying method bodies. Any other modification is rejected by the compiler.
  • C# Hot Reload for Skia supports modifying method bodies, adding properties, adding methods, adding classes. A more accurate list is provided here in Microsoft's documentation.

Troubleshooting Uno Platform VS Code issues

If you're not sure whether your environment is correctly configured for Uno Platform development, running the uno-check command-line tool should be your first step.

The Uno Platform extension provides multiple output windows to troubleshoot its activities:

  • Uno Platform, which indicates general messages about the extension
  • Uno Platform - Debugger, which provides activity messages about the debugger feature
  • Uno Platform - Hot Reload, which provides activity messages about the Hot Reload feature
  • Uno Platform - XAML, which provides activity messages about the XAML Code Completion feature

If the extension is not behaving properly, try using the Developer: Reload Window (or Ctrl+R) command in the palette.

Getting Help

If you continue experiencing issues with Uno Platform, please visit our GitHub Discussions or Discord - #uno-platform channel where our engineering team and community will be able to help you.

C# Dev Kit Compatibility

At this time, the preview version of the C# Dev Kit extension ms-dotnettools.csdevkit is not compatible with the Uno Platform extension. It requires a preview version of the C# extension ms-dotnettools.csharp that contains major breaking changes.

Workaround

You can use both the Uno Platform and C# Dev Kit extensions but not simultaneously. The easiest way to accomplish this is to create profiles inside VSCode. Using this method, you can:

  1. Create one profile for Uno Platform

  2. Disable, if installed, C# Dev Kit extension

  3. Enable useOmnisharp inside the configuration useOmnisharp

  4. Create another profile for C# Dev Kit

  5. Enable (or install) the C# Dev Kit extension

  6. Ensure that useOmnisharp is disabled inside the configuration

  7. Disable the Uno Platform extension

You can then switch between both profiles according to the type of dotnet project you are developing.