Running your app on a Raspberry Pi with Raspbian Lite
This tutorial shows you how to run your Avalonia app on a Raspberry Pi with Raspbian Lite via DRM.

Step 1 - Setup the Raspberry Pi

First step is to setup your Raspberry Pi.

1.1 Download the Raspbian lite operation system image.

You can download the Raspbian lite operating system image from the official Raspberry Pi website. Link to Raspberry Pi Operating system imagesโ€‹

1.1 Prepare Raspberry for flashing

The installation of Raspberry Lite is a bit different depending on the model.
โ€‹Raspberry Pi 4 b For the Pi 4 b you need a SD Card on which the operating system will be installed. Insert the SD card into your computer. You can now go directly to step 1.2.
โ€‹Raspberry CM4 Since the CM4 is designed for embedded applications you still need an IO board. For this there is the official Compute Module 4 IO board or alternatively many other boards like the SourceKit PiTray mini.
To prepare the EMMC memory for mounting follow these steps.

1.2 Flashing the operating system

  • โ€‹Download the Etcher image writing utility and install it.
  • Open Etcher and select from your hard drive the .zip file you downloaded in step 1.1.
  • Select the mass storage (SD card or CM4 EMMC) you wish to write your image to.
  • Review your selections and click 'Flash!' to begin writing data. Once flashing is complete, create a new empty file named ssh (with no extension, e.q with touch ssh) in the boot drive of the Raspberry. This will ensure that the SSH daemon is enabled once the Raspberry Pi has started and you can logon over the network.
  • Only for Cm4 add the following to /boot/config.txt to enable the Usb 2.0 ports
1
dtoverlay=dwc2,dr_mode=host
Copied!
  • Start up your Raspberry and login. Raspberry Pi 4 b: Put the SD card into the Raspberry and plug in power supply CM 4: On CM4 IO Board unplug the power supply, remove J2 jumper, plug in power supply again

1.3 Install missing libraries

Some libraries required to run a Avalonia app via DRM on raspbian lite:
1
sudo apt update
2
sudo apt upgrade
3
sudo reboot
4
sudo apt-get install libgbm1 libgl1-mesa-dri libegl1-mesa libinput10
Copied!

1.4 Verify DRM (optional)

You can test your installation with a simple but useful tool called kmscube.
1
sudo apt-get install kmscube
2
sudo kmscube
Copied!
You should see the spinning cube on your Raspberry pi screen now: โ€‹
โ€‹
โ€‹

Step 2 - Prepare Avalonia App

2.1 Create new Avalonia App (Core or MVVM App) We called it AvaloniaRaspbianLiteDrm in this tutorial.
2.2 Add package Avalonia.LinuxFrameBufferโ€‹
1
dotnet add package Avalonia.LinuxFramebuffer
Copied!
2.3 Create MainView When we work via FrameBuffer there are no windows, so we need a separate view (UserControl) which will be our toplevel control. This view is the counterpart to the normal window.
MainView will be our app base in which we develop our UI:
1
<UserControl xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5
mc:Ignorable="d"
6
d:DesignWidth="800"
7
d:DesignHeight="450"
8
x:Class="AvaloniaRaspbianLiteDrm.MainView">
9
<StackPanel HorizontalAlignment="Center"
10
VerticalAlignment="Center"
11
Margin="30"
12
Spacing="30">
13
<TextBlock FontSize="25">
14
Welcome to Avalonia! The best XAML framework ever โ™ฅ
15
</TextBlock>
16
<Slider />
17
</StackPanel>
18
</UserControl>
Copied!
Now create a new UserControl with name MainSingleView and host the MainView:
1
<UserControl xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5
xmlns:avaloniaRaspbianLiteDrm="clr-namespace:AvaloniaRaspbianLiteDrm"
6
mc:Ignorable="d"
7
d:DesignWidth="800"
8
d:DesignHeight="450"
9
x:Class="AvaloniaRaspbianLiteDrm.MainSingleView">
10
<avaloniaRaspbianLiteDrm:MainView />
11
</UserControl
Copied!
Also change the MainWindow.axaml to host the MainView inside:
1
<Window xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5
xmlns:avaloniaRaspbianLiteDrm="clr-namespace:AvaloniaRaspbianLiteDrm"
6
mc:Ignorable="d"
7
d:DesignWidth="800"
8
d:DesignHeight="450"
9
x:Class="AvaloniaRaspbianLiteDrm.MainWindow"
10
Title="AvaloniaRaspbianLiteDrm">
11
<avaloniaRaspbianLiteDrm:MainView />
12
</Window>
Copied!
So as you see the MainView is hosted in booth MainSingleView and MainWindow. This makes it easier for development to run the app also on desktop and on the Raspberry
2.3 Prepare Program.cs Next we need to prepare the Program.cs to enable the DRM usage. Change the Main void to the following:
1
public static int Main(string[] args)
2
{
3
var builder = BuildAvaloniaApp();
4
if (args.Contains("--drm"))
5
{
6
SilenceConsole();
7
return builder.StartLinuxDrm(args);
8
}
9
โ€‹
10
return builder.StartWithClassicDesktopLifetime(args);
11
}
12
โ€‹
13
private static void SilenceConsole()
14
{
15
new Thread(() =>
16
{
17
Console.CursorVisible = false;
18
while (true)
19
Console.ReadKey(true);
20
})
21
{ IsBackground = true }.Start();
22
}
Copied!
SilenceConsole() captures the console input and hide it. Otherwise you will see the console cursor blinking on the screen.
2.4 Prepare App.axaml.cs Next we need to set the MainView for the ISingleViewApplicationLifetime for the DRM usage.
Change the OnFrameworkInitializationCompleted() in App.axaml.cs:
1
public override void OnFrameworkInitializationCompleted()
2
{
3
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
4
desktop.MainWindow = new MainWindow();
5
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleView)
6
singleView.MainView = new MainSingleView();
7
โ€‹
8
base.OnFrameworkInitializationCompleted();
9
}
Copied!
2.5 Run and test on desktop Now you can run/debug your app on desktop as usual. When you start your app you should see this: โ€‹
โ€‹
โ€‹

Step 3 - Deploy and run on Raspberry

3.1 Publish app
1
dotnet publish -c Release -o publish -r linux-arm -p:PublishReadyToRun=true -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true
Copied!
3.2 Copy app to Raspberry Copy the files from /publish directory of your project to your Raspberry. You can do this via scp <source> <destination> or use a app like CyberDuck or via Usb stick.
3.3 Run app on Raspberry First we need to change the permission to executable.
1
sudo chmod +x /path/to/app/AvaloniaRaspbianLiteDrm
Copied!
Now you can run the app with the following:
1
sudo ./path/to/app/AvaloniaRaspbianLiteDrm --drm
Copied!
You should see the app running on your Raspberry Pi now: โ€‹
โ€‹
โ€‹
If you have a touch display installed, try to slide the slider control :)