Setting up a Raspberry Pi to run a .NET Core GUI App with Avalonia

February 12, 2024


Contents


Introduction

Here are the steps to set up a monitor that displays an Avalonia UI application running on a Raspberry Pi. Avalonia UI is a framework that can be used to create cross-platform applications with .NET Core.

This assumes you have created an Avalonia app using .NET 7.

NOTE: This method uses the Openbox window manager and not Avalonia's DRM capabilities.


Hardware

  • Make sure you have everything required to run a raspberry pi 3 or 4.
  • To set up the wall display, you'll need and a monitor and HDMI cable.
  • To set it up as a kiosk, you'll need a keyboard and mouse. (optional)

Install Raspberry Pi OS on the SD card

I used Raspberry Pi Imager v1.8.5 to put Raspberry Pi OS Lite (32-bit) (2023-12-05 release) onto an SD Card.

The Imager program should provide "OS Customisation" options to configure credentials and enable SSL.

When the SD card is ready, insert it into the device and power it on.


Basic configuration

Find the rapberrypi IP address and SSH into it.

Type sudo raspi-config

  1. (optional) Change your password
  2. (optional) Under Network Options, change the hostname of the device
  3. Under Boot Options, set it to "Console Autologin" (Open option B1 then B2)
  4. (optional) Under Localisation Options

(I1) Change locale to en_US.UTF-8
(I2) Change time zone to US - Central
5. (optional) Under Advanced Options
(A1) Expand filesystem
(A2) Overscan -> No

Select Finish

Restart the device using the command sudo reboot


Update Linux

Update all packages
sudo apt-get update -y
sudo apt-get upgrade -y


Install .NET SDK (optional) (reference)

  1. Install the .NET SDK

curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --version 7.0.405
2. Add a DOTNET_ROOT environment variable and add the .dotnet directory to $PATH
echo 'export DOTNET_ROOT=$HOME/.dotnet' >> ~/.bashrc
echo 'export PATH=$PATH:$HOME/.dotnet' >> ~/.bashrc
source ~/.bashrc
3. Verify the .NET installation
dotnet --version


Install Sqlite (optional)

sudo apt-get update
sudo apt-get install sqlite3 libsqlite3-dev


Publish and Deploy the Avalonia App

  1. On development machine, create a framework-dependent deployment. (For 64-bit installations, replace linux-arm with linux-arm64.)

cd C:\Projects\myapp\MyAvaloniaApp
dotnet publish -r linux-arm --self-contained --configuration Release

  1. Copy files in the publish folder to the target machine

On the Target machine:
mkdir ~/myapp

On the Dev machine:
cd C:\Projects\myapp\MyAvaloniaApp\bin\Release\net7.0\linux-arm\publish
scp -r ./* pi@10.19.4.16:/home/pi/myapp

  1. Set permissions

cd ~/myapp
sudo chmod +x MyAvaloniaApp


Install and Configure Openbox

  1. Keep the device from going to sleep

Enter the command sudo nano /etc/rc.local to edit the rc.local file
Add line /sbin/iw wlan0 set power_save off above exit 0
You can check if power save is on with the command /sbin/iw wlan0 get power_save

  1. Install Openbox window manager

sudo apt-get install --no-install-recommends xserver-xorg x11-xserver-utils xinit openbox

  1. Edit Openbox config

sudo nano /etc/xdg/openbox/autostart
Add the following text:

xset -dpms      # turn off display power management system
xset s noblank  # turn off screen blanking
xset s off      # turn off screen saver

# Run the Avalonia app
/home/pi/myapp/MyAvaloniaApp

  1. Start the X server on boot

sudo nano ~/.bash_profile
Add the following line:

[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && startx -- -nocursor

  1. Load the bash profile

source ~/.bash_profile
6. Reboot
sudo reboot

After rebooting, the Avalonia app should automatically start up.


Mount your display

After confirming that everything is working, you can shut down the device and mount it in its final location.

To shut down, use sudo halt. Do not unplug the raspberry pi while it is running because there is a chance that the SD card could get corrupted.