User Tools

Site Tools

한국어

comfilepi:running_.net_winforms_applications_with_mono:index

Running .Net WinForms Applications with Mono

Overview

.Net applications can run on Linux by utilizing the Mono Framework. The Mono Framework is already installed on the ComfilePi's default Raspberry Pi OS image.

WinForms applications can be developed and debugged on a Windows PC using Visual Studio, and then simply copied to the ComfilePi and executed with mono; no re-compiling required!

Mono implements desktop Winforms, so, unlike the .Net Compact Framework and Windows CE, Mono and the ComfilePi support alpha-blending, anti-aliasing, and other rich UI features.

Mono provides the basic System.Windows.Forms implementation. However, there are many graphics and UI frameworks that are compatible with Mono that can also be used. Below are a few potential offerings.

  • AdvancedHMI - HMI controls for .Net
  • QtSharp - Mono/.NET bindings for Qt.
  • XWT - A cross-platform UI toolkit for creating desktop applications with .NET and Mono.
  • Eto.Forms - A cross platform GUI framework for desktop and mobile applications in .NET.
  • OpenTK - A set of fast, low-level C# bindings for OpenGL, OpenGL ES and OpenAL.
  • Gtk# - A graphical user interface toolkit for mono and .Net. The project binds the gtk+ toolkit and assorted GNOME libraries, enabling fully native graphical Gnome application development using the Mono and .Net development frameworks.

There are a few plugins that one can use to remote debug .Net/Mono programs running on the ComfilePi from Visual Studio, but they are all in various states of incompleteness or disrepair. Instead it is recommend to debug applications on the Windows host PC before deploying to the ComfilePi. In the unusual case where a program must be debugged on the ComfilePi, one can use the SDB command-line debugger.

NOTE: Mono is not a perfect implementation of the .Net Framework (though it's quite good). You may need to make some adjustments to your code or choose Mono-compatible libraries in some situations, but those situations are typically the exception, not the rule.

Mono does not support WPF applications.

Install Mono on the ComfilePi

Check if Mono is already installed. ​ Run the command ​mono -V​ to verify.

$ mono -V 		
Mono JIT compiler version 4.8.0 (Stable 4.8.0.495/​e4a3cf3 Wed Feb 22 18:27:02 UTC 2017) 		
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com 		
		TLS:           ​__thread 		
		SIGSEGV: ​      ​normal 		
		Notifications:​ epoll 		
		Architecture: ​ armel,​vfphard 		
		Disabled: ​     none 		
		Misc:          softdebug 		
		LLVM:          supported, not enabled. 		
		GC:            sgen 		
		

If it isn't installed, it can be installed with sudo apt install mono-complete or by following the Mono Project's installation instructions for the Raspberry Pi.

If you are using Visual Basic, then you will likely also want to install ​mono-vbnc​ for dependencies such as ​Microsoft.VisualBasic​.

sudo apt-get install mono-vbnc 		

Demonstration

This documentation will take you through all of the steps necessary to set up the development environment on a Windows PC, build a .Net application, and deploy and execute it on the ComfilePi.

The following video provides a brief illustration of the procedure.

Install Visual Studio on Windows PC

Download the Visual Studio installer from https://www.visualstudio.com/. Be sure to select the .Net desktop development component during the installation.

Create a WinForms Project

Create a new C# WinForms application by selecting Windows Classic Desktop–>Windows Forms App (.NET Framework) project template. Name the project Hello World.

Add a Button and a Label control to the form. Change the button's Text property to Say Hello.

Double-click the button to add a click event handler as shown in the code below.

using System;
using System.Windows.Forms;
 
namespace HelloWorld
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "Hello, World!";
        }
    }
}

Debug the Application On the Windows PC

Press Start in the Visual Studio toolbar to execute and debug the program.

Press the Say Hello button. The button1_click event handler will fire, and the label will change to read Hello, World!.

Deploy the Application and Execute it on the ComfilePi

The typical way to connect to the ComfilePi from a Windows Desktop PC is via Ethernet. The ComfilePi has built-in Ethernet port, and once connected to a network it will automatically obtain an IP address via DHCP.

Obtaining the ComfilePi's IP Address

To obtain the IP address, run hostname -I from a command console, or touch or float the mouse cursor over the Wireless & Wired Network Settings item from the ComfilePi's desktop panel.

Enable the ComfilePi's SSH Server

Typically, a remote connection between the Windows PC and the ComfilePi is established via SSH. Be sure the SSH Server is enabled on the Comfile Pi. Enabling the SSH server also enables an SFTP server for copying files.

Copying Files via SFTP

At this time, Windows does not include an SSH client, so 3rd party utilities are needed. For this demonstration we'll use the FileZilla Client to copy our executable files from the Windows PC to the ComfilePi. Download the FileZilla Client and install it on the Windows PC.

Once installed run the FileZilla Client and connect to the ComfilePi by entering its IP address, username (default: pi), password (default: raspberry) and port (22 for SFTP), and pressing the Quickconnect button.

Drag and drop the files from the Windows PC to the ComfilePi.

Execute the Application on the ComfilePi

To execute the application, simply run the command mono HelloWorld.exe.

Optional: Automating the Deployment and Execution on the ComfilePi

The procedure above can be tedious and inefficient while one is iterating on the development of their software. In the steps below, we will automate the process with the pscp and plink utilities and Visual Studio's post-build event.

Deploying the Application to the ComfilePi

  1. Download both pscp.exe and plink.exe from http://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html, and place them somewhere in your Windows PC's PATH.
  2. Add the following commands to the Post-build event command line.
    plink -no-antispoof -pw raspberry pi@192.168.233.18 mkdir -p $(ProjectName)
    pscp -r -pw raspberry "$(ProjectDir)$(OutDir)\*" pi@192.168.233.18:$(ProjectName)


    The plink command will ensure a folder home/pi/$(ProjectName) exists on the ComfilePi, and the pscp command will upload the necessary binary files to that folder on the ComfilePi.

Execute the Application on the ComfilePi

  1. Open the project's Configuration Manager.

  2. Create an new configuration call ComfilePi.

  3. With the ComfilePi configuration selected, add the following to the project's Debug properties.

    That command will execute export DISPLAY=:0.0 on the ComfilePi to ensure the program shows itself on the correct X display, and then executes the command mono HelloWorld/HelloWorld.exe to execute the program.

  4. Select Start from the Visual Studio toolbar to execute the application remotely on the ComfilePi.

Debugging the Application on the ComfilePi

There are some Mono debugging extensions for Visual Studio, but they are not always maintained.

Another way to debug Mono projects running on the ComfilePi is to load the solution folder in Visual Studio Code, and use Microsoft's Visual Studio Code Mono Debugger:

Source Code

Download the source code for this demonstration.

helloworld.zip

comfilepi/running_.net_winforms_applications_with_mono/index.txt · Last modified: 2024/04/01 15:27 by COMFILE Technology