Develop graphical Linux apps on Windows with the WSL, Visual Studio and Mono

Develop graphical Linux apps on Windows with the WSL, Visual Studio and Mono

In this guide, I will present you the Windows Subsystem for Linux (WSL) and how to develop graphical Linux applications on Windows with Visual Studio, thanks to Mono used as the runtime.

First you need to check that your Windows version is at least 1607 (Falls Creator Update). Check your Windows version from Settings | System | About:
01.-About

Then you need to enable Developer mode on Windows, from Settings | Update & Security | For developers, and switch to developer mode:
02.-Developer

Now you can enable the WSL from Start | Turn Windows features on or off, choose "Windows Subsystem for Linux" (this will make your machine to reboot).
03.-Linux-subsystem

From the Microsoft Store, it's time choose your favorite Linux distro. In my case, I will use Debian, but other choices are also possible:
04a.-Debian
04b.-Debian

Lunch the app from the store, it will finish the initial configuration.
It will ask you to create a local user (completely disconnected from your Windows account).
05.-User

Update the Debian installation with the following command:

sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y

Install on your machine an X-server, for example VcXsrv.
Start it for the first time (you can also choose to auto-start it in Windows).

In the Linux subsystem, configure the local X server with the following command:

echo "export DISPLAY=localhost:0.0" >> ~/.bashrc

Restart bash, or run the following command:

. ~/.bashrc

In Visual Studio, create now a regular Windows Forms app.
In my case, I place on the main form a button and a label, and on the button click I update the label text with the current time:

private void Button1_Click(object sender, EventArgs e)
{
	label1.Text = DateTime.Now.ToLongTimeString();
}

We start our app on Windows, nothing surprising:
06.-WinForm-on-Windows

Let's install Mono on Linux:

sudo apt install mono-complete

From the Linux shell, browse to the folder containing the output of your app (the C disk is mounted under /mnt/c), then start the app with Mono:

cd /mnt/c/_Works/WinFormsTest/bin/Debug/
mono WinFormsTest.exe

07.-WinForm-on-Linux

In case of issues when starting the Mono app in Linux, try to run the following command:

sudo apt --fix-broken install

References: