In the previous article we looked at the options for developing cross-platform applications in C# programming. The technology that makes this possible is Mono from Novell. In this article we'll take an existing Windows C# application and run it on both Mac OS X and Linux. Before we dive into the details it's important to take a quick look at what Mono is and where it came from to help you understand what's happening behind the scenes.
The Mono project has been around for almost nine years and started out as an effort to implement an open-source version of the ECMA-standard (ECMA-334) C# programming language and the companion Common Language Infrastructure or CLI (defined in ECMA-335). These standards correspond with Microsoft's products commonly known as the .NET framework platform. Programs written with Mono run on different platforms through the use of native run-times. If fact, you can run the exact same .EXE file on any supported platform with the Mono runtime installed.
Version 2.0 of the Mono runtime was the first release to provide robust support for native Windows Forms applications. That basically means that Novell implemented all of the
System.Windows.Formsnamespace. Under the cover, Mono uses the
System.Drawinglibrary along with drivers for X11, Win32 and OS X to do the heavy lifting. While Windows Forms applications will run on any supported platform, you'll still have to use Microsoft Visual Studio for the actual design and coding if you want to use a visual designer. We'll discuss the Mono Visual Studio add-in a little later.
While Mono makes it possible to run Windows Forms-based apps on virtually any OS, it doesn't mean they will look great on another platform. In many cases the look and feel will be adequate, but there may be some cases where the differences are significant. The Mono team has put together a guide on their website that walks you through the process of porting an application using the Mono Migration Analyzer (MoMA) to determine potential issues.
To test out MoMA we downloaded a number of Windows Forms samples from the windowsclient.net website. The RegexTest sample illustrates an issue that tends to pop up between platforms. When you run the MoMA tool against the RegexTest.exe file, it gives the following warning:
Setting this is probably unintentional and can cause Forms to be improperly sized. See http://www.mono-project.com/FAQ:_Winforms#My_forms_are_sized_improperly for details.
The result when you run the application on Linux is a chopped off window that doesn't fully show the buttons at the bottom of the form. Figure 1 shows what it looks like on Windows, and figure 2 shows the same application running on Linux.
Figure 1 RegexTest application running on Windows
Figure 2 RegexTest application on Linux
The fix is a simple one in this case. All of the visual components of this application are built in code, and the line that sets the size looks like this:
this.ClientSize = new System.Drawing.Size(536, 503);
Changing it to the following fixes the problem:
this.ClientSize = new System.Drawing.Size(536, 536);
Figure 3 Updated RegexTest app running on Linux.
Things to Watch For
One of the things that can cause problems when porting apps between operating systems stems from subtle differences like file naming conventions. Windows filenames are case insensitive while both Mac OS X and Linux are case sensitive. This can lead to issues if you have filenames for things like resources or images referenced either in code or in a configuration file that aren't consistent. You must also deal with the different usage of the "\" versus the "/" in path names. Mono does offer a feature they call
IOMap to specifically help with the path issue.
If you happen to be a Ubuntu, user you'll find the latest version of Mono available through the normal update channels is 2.4.4. While that shouldn't be a big issue, you could run into some problems if you're using any of the Language Integrated Query (LINQ) features. The latest version of Mono also begins to implement some of the C# 4.0 features along with ParallelFX, although you'll have to wait for at least 2.8 to get started with those.
One option for getting Mono 2.6 up and running on Ubuntu 10.4 is to build it from source. There are instructions on the Mono site on how to do it, but the process is not for a beginner. A quick Google search turned up a blog post about installing Mono 2.6.x in parallel on Ubuntu 9.10. You'll need to have subversion installed along with libtool to get the script to work properly.
One of the things we didn't cover in these two articles is the ability to write C# code using Mono for Apple's iPhone, iPod Touch and iPad. While you won't be able to take your Windows Forms apps to those platforms, you will be able to code in C#. It requires you to do all the coding on a Mac and use the Apple SDK. Building the actual interface can be done either programmatically or by using Apple's Interface Builder. Future versions of this same tool are planned for Google's Android platform as well.
Novell's Mono product is essentially the only game in town if you want to take an existing Windows Forms app and run it natively on Linux or Mac. It's also the primary force behind a number of popular open source apps like the Banshee media player and F-Spot photo manager. Mono is definitely worth a look if you have a need for cross-platform apps.