Targeting Both .NET and Mono

By Jeff Cogswell  |  Posted 2010-02-23 Print this article Print


Targeting Both .NET and Mono

In order to run MonoDevelop 2.2 on Windows, you need to first download and install GTK#, which is a set of .NET bindings for GTK+, the cross-platform open-source GUI tool popular on Linux and several other platforms. That installation is quick and painless, and once done, you then download and install MonoDevelop 2.2, which is also quick and painless. (You don't have to install Mono itself if you don't want to, in which case you'll be developing for .NET instead of Mono. Yup, you read that right.)

And with MonoDevelop, you can target both Mono and the built-in Microsoft .NET framework. When you run your compiled program, there's a menu option that lets you choose which platform to run it under. To make this work, though, you need to download and install the complete Mono runtime, which is a separate download from MonoDevelop. (Otherwise you won't see multiple choices in the menu; you'll only see one for Microsoft .NET.)

However, technically, you're not exactly "targeting" different platforms; your executable file doesn't change based on the target you choose. The only difference is that your executable either runs by itself (which puts it under the Microsoft .NET platform) or it runs as a parameter to the Mono.exe program, which runs it under the Mono platform. The executable itself isn't built or compiled differently based on the two platforms. The reason that works is the executable is actually a .NET assembly that contains compiled bytecode stored as data. When you run a .NET executable, there's some startup code that's also present in the executable file that executes, launches the .NET runtime and then runs the bytecode stored in the data.

If instead you run the Mono.exe program, passing your executable as a parameter, the startup code isn't run-Mono.exe launches the Mono runtime, which then reads the exact same bytecode and runs it under Mono. Voila! (In fact, you can take most .NET executables and run them under Mono by simply passing the executable name as a parameter to the Mono.exe program.)

Nice Window Designer

The window designer is quite nice. Similar to Visual Studio and other RAD (rapid application development) tools such as Delphi, you have a window and a toolbox, and you can drag controls from the toolbox onto the window, and then set their properties. At the bottom of the source code window for window classes are two tabs, Source Code and Designer. Technically, these don't give you two different views of the same thing; rather, Designer is for layout, and Source Code is for accompanying code such as button event handlers.

For some reason, by default, the Toolbox and Properties windows weren't present when I opened Designer. But they were easy to find under the View menu. And if you're coming from a Visual Studio background, you might be a little startled to be working in GTK# instead of the usual WinForms world of Visual Studio. With MonoDevelop, by default your windowing applications target GTK#, and so the toolbox is arranged accordingly.

The layout mechanism works as per GTK# rules. You can't just drop a button on the form; first you have to drop a layout and then drop your controls. And similarly, when you want to create, for example, a button handler, you don't double-click the button on the form (nothing happens). Instead, you go to the Properties window and click the Signals tab. (In the GTK world, you use signals, which are effectively the same as events.) But this is easy: You just double-click the event-er, I mean signal-name, and Designer will switch to your code view and automatically insert a handler function for you that you can fill in with your own code.

But if you're watching closely, you might wonder something regarding GTK# and the .NET framework. I mentioned that ultimately you're building a .NET executable that contains .NET startup code and compiled bytecode. But what if you're using GTK#? That's not automatically part of .NET, right? Correct, but you're fine. GTK# can run under .NET as well as Mono. And that means your compiled GTK# GUI application can run under either .NET or Mono. Of course, that does introduce a caveat: If you plan to deploy your GUI application, the target machine needs to have GTK# installed. (I'd like to petition Microsoft to ship GTK# with all Windows machines. One can dream.)

Jeff Cogswell is the author of Designing Highly Useable Software ( among other books and is the owner/operator of CogsMedia Training and Consulting.Currently Jeff is a senior editor with Ziff Davis Enterprise. Prior to joining Ziff, he spent about 15 years as a software engineer, working on Windows and Unix systems, mastering C++, PHP, and ASP.NET development. He has written over a dozen books.

Submit a Comment

Loading Comments...
Manage your Newsletters: Login   Register My Newsletters

Rocket Fuel