Cross Platform Development with Mono

Recently I moved my main home PC and Laptop over to the Linux Mint distribution, which for someone who has been a dedicated Windows users since I ever got my hands on a PC back around 1990, has gone amazingly well. My next little venture is to do a little cross platform development. Although I have moved away from Windows (not strictly true as I have set up Windows 8 in a Virtual Box environment in-case I need to switch over for something) I still really like the .NET environment and C# language, so I would like to carry on using it. I would like to learn another language like Python / Ruby, but I really don’t have the time at the moment as I have just changed job and I also have young kids at home which takes up a lot of my spare time.

Mono Development with C#

Mono Development with C#

Thankfully, there is the Mono .NET implementation that I can use and it is really well supported. Mono is a Free (as in freedom) implementation of the C# language and .NET runtime that is written to the ECMA-334 open specification. As well as being open source, Mono also has a company, Xamarin, sponsoring its development as they use the mono system for their IOS and Android application development system, of which you can also use for free (within limits).

My main interest at the moment is writing desktop applications for both Linux and Windows. Mono does support Windows Forms, but it isn’t as well supported as the Linux Gnome user interface library GTK. Mono has a version called GTK# and it is designed to be cross platform so you can use it with Windows and Mac OSX as-well.

My SafePad and Text Shredder applications where both developed using the Windows Forms library and they both run perfectly fine under the Mono Runtime on Linux. The user interface rendering looks a little odd on Linux as it is a Windows UI library, but the applications fully work.

For my first cross platform test I decided to write a very simple application, a basic text editor. I started the application during my lunch break at work, so I started the development using Xamarin Studio under Windows 7 on my work computer, and then did the 2nd half of the development using MonoDevelop under Linux Mint at home.

 Xamarin Studio

Xamarin Studio

Xamarin Studio is essentially MonoDevelop with a little re-branding from the Mono Project sponsor. Xamarin Studio is the main development IDE if you want to use .NET to create IOS and Anroid .NET applications. Apart from the cosmetic differences, this IDE is the same as MonoDevelop under Linux. To create a desktop application, you select a GTK# application under C# when you create a new solution. Mono Develop even includes the Stetic Form Designer so you can visually create your forms.

Xamarin Studio

Xamarin Studio

Creating forms isn’t as straight forward as the Windows Forms designer under Visual Studio as all the controls have to be constrained in containers, where as they don’t under Windows Forms. Once you get used to this, then the form designer is quite straight forward. For this simple little text editor I had to put a Vbox control on the form first which split the window into 3 sections. I then inserted a 4th section, by right clicking on one of the panels and selecting insert. In the top section I placed the menu. Under that a toolbar. Below that a text box window, and below that a status bar.

The menus were easy to define as where the buttons on the toolbar. You can also add icon graphics from the standard gnome icon library. When developing under Windows, your application builds and runs against the standard .NET runtime. Here is the text editor application running under Window 7.

Application running on Windows

Application running on Windows

Xamarin Studio / Mono Develop is quite a nice IDE to use. I was pleasantly surprised, but what if you still want to use Visual Studio. Well you can. Xamarin Studio created standard Visual Studio solution and project files, so the project loaded up, built and ran fine.

GTK# Development in Visual Studio 2013

GTK# Development in Visual Studio 2013

What you don’t get though is the Stetic Forms Designer. If this is a deal breaker then just stick with Xamarin Studio / Mono Develop for building your forms. The other option is hand coding your user interfaces, which some people will probably prefer. I know some Windows Forms developers who don’t use the form designer and hand crank all their UI code. As well as working in Visual Studio, you can also load up the solution and run it from Sharp Develop. MonoDevelop is actually based on a fork of the SharpDevelop code-base to allow it to be made cross platform.

GTK# Development in Sharp Develop

GTK# Development in Sharp Develop

I did the 2nd half of the development for this test application in MonoDevelop in Linux itself. Everything worked straight out of the box when I transferred the source code over. Below is a screen-shot of the application running.

Application Running on Linux

Application Running on Linux

Visually the application is similar, but it has more of the gnome feel to it. Especially with the icons. MonoDevelop functionally is the same as Xamarin Studio and includes the same Stetic Forms Designer.

MonoDevelop running under Linux

MonoDevelop running under Linux

To install MonoDevelop under Linux is very straightforward. You need to install the mono runtime, and monodevelop. This can be done via the software manager or via the command line with:

sudo apt-get update
sudo apt-get install mono-complete
sudo apt-get install monodevelop

MonoDevelop Stetic GTK# UI Editor

MonoDevelop Stetic GTK# UI Editor

Although this is a very simple test application is has helped me get used to the programming environment for Mono under Linux and serves as a good introduction to basic GTK# development. For my first larger project, I am planning on changing my SafePad application to use GTK# instead of Windows Forms. Even though the application runs perfectly well in it’s current state under Linux, I think that now I would feel better if it was using GTK# as this will pave the way for some more cross platform development work.

You can download the source files for this test application from here.

This entry was posted in Coding, Linux, Mono, Open Source and tagged , , , . Bookmark the permalink.

11 Responses to Cross Platform Development with Mono

  1. rio says:

    Nice writeup, motivates me to try C# on Linux as well. Btw. source code link seems to be broken …

    • Stephen Haunts says:

      Hmm interesting. Just tried the link on my tablet and it was OK. The file is in a public drop box folder. I will take a closer look when I get home.

    • Stephen Haunts says:

      Just checked the link and I definately copied the public file link. I have updated it with the shortened link, but still resolved to the same file. Give it another try.

  2. noko says:

    It’s missing WPF, and there’s no plans to support it, so there goes cross platform C#. I’d love to use C# with Qt instead but all bindings to C# look dead.

  3. Rehan khan says:

    Hello i just want to ask you it is possible to create desktop application in xamarin studio and run both on windows or linux enviroment perfectly ??

    • Stephen Haunts says:

      It is yes. You can use windows forms, but the forms supoprt in mono is not fantasic. The better option for cross platform application dev is probably to use GTK#.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s