I’ve spent the majority of my career focused entirely on open source Web development, having only occasionally dabbled in .NET development. Of course, the great thing about the open source community is that if you do happen to need a particular application, chances are somebody else has already created it. But what if it doesn’t exist, or if an existing application doesn’t work precisely as you believe it should?
I recently encountered this very problem when searching for streaming radio applications (plenty such applications exist, I just don’t like any of them), and concluded that perhaps the only way to scratch this particular itch was to build my own application. This decision immediately prompted two questions: how in the heck are Linux desktop applications built in the first place, and could I harness my minimal .NET experience in the process?
As it turns out, there are quite a few open source solutions, but one in particular happens to be .NET-specific. That solution is Mono, an open source, cross platform C# implementation. In this article I’ll document how I’ve begun using Mono.
Installing Mono
Several Ubuntu applications are built atop Mono, including Banshee and F-Spot. Because both are installed by default on Ubuntu, so is Mono. However if you’re running another platform, or had previously uninstalled Mono, you can install it either via your Linux distribution’s package manager, or by downloading it from the Mono website. Versions are available for Windows, OS X, SLES, and a slew of Linux distributions. Once installed you can make sure everything is running properly by opening a terminal and running the Mono compiler:
mono --versionMono JIT compiler version 2.4.4 (Debian 2.4.4~svn151842-1ubuntu4)Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.com TLS: __thread GC: Included Boehm (with typed GC) SIGSEGV: altstack Notifications: epoll Architecture: x86 Disabled: none
Choosing a Mono IDE
After installing Mono (or making sure it is installed), the next step is to find a capable IDE. The Mono website highlights two IDEs in particular: MonoDevelop, an open source, cross-platform IDE, and Visual Studio, Microsoft’s flagship .NET development suite (by way of the Mono Tools Add-in). I chose the former, installing it via Ubuntu’s package manager, and so will concentrate specifically on MonoDevelop for the remainder of this article.
Other IDEs also offer Mono support, including notably Eclipse. See this summary for a complete list of available options.
Creating Your First Mono Project
Upon firing up MonoDevelop for the first time, I was surprised to see it was possible to create a GTK-based project (see Figure 1). The Mono-specific version is called Gtk# (Gtk Sharp), giving developers access to a rich suite of cross-platform widgets.
Figure 1. Creating a Gtk# 2.0-based Project
After creating the new project, MonoDevelop will automatically provide the C#/Gtk# equivalent of “Hello, world” as a starting point. However, attempting to compile and run the skeleton application produced a number of errors regarding missing assemblies, all of which were pertinent to Gtk#. Additional research indicated that Gtk# is not installed by default alongside Mono. Another trip to the Synaptic Package Manager fixed the problem, and I subsequently successfully compiled and ran the application (see Figure 2).
Figure 2. Running “Hello, World”
The next step involved adding a few interface elements to serve as starting points for subsequently adding features. Fortunately, MonoDevelop is equipped with a great interface designer, which you can access by clicking the Designer
button located at the bottom of the editor (see Figure 3).
Figure 3. Click the Designer Button to Open the Interface Designer
When clicked, a window containing almost fifty interface elements will appear. To design the interface, all you need to do is drag and drop the desired elements atop the window. If you’re not familiar with the behavior of layout managers, take the time to read this tutorial explaining Gtk#’s layout approach. After adding a menu bar and status bar, I was starting to see glimpses of my application prototype (Figure 4).
Figure 4. Adding a Menu and Status Bar
Wiring Events to the Controls
Continuing with the approach of continuous small victories, I next decided to implement the Quit
command found in the File
menu, and create a project description that appears when the user clicks the About RadioRadio
command found in the About
menu. Let’s start with Quit
.
Open the main window’s interface designer and click the Quit menu item. When you do, the Properties window will appear (Figure 5).
Figure 5. Setting the Quit Menu Item’s Activated Signal
We want to tie a signal to the Activated
event. When clicked, the method associated with the event will execute. Associate the name OnQuit
with the Activated
event. When you do, a method named OnQuit
will automatically be added to the MainWindow.cs
source. All you need to do is add the Application.Quit()
call, as depicted here:
protected void OnQuit (object sender, System.EventArgs e){ Application.Quit();}
Next, let’s create the about dialog. A special type of window called an AboutDialog
is native to Gtk#. It allows you to easily package all of the usual information about your application into a simple popup window, including the title, author and copyright data. Attach a method called OnShowAbout
to the About
menu item, and modify the OnShowAbout
method so it looks like this:
protected virtual void OnShowAbout (object sender, System.EventArgs e){ AboutDialog about = new AboutDialog(); about.Title = "About RadioRadio"; about.Copyright = "Copyright 2011 W. Jason Gilmore"; about.Show();}
Rebuild and run the application. When you click on the About
menu item, you’ll be greeted with the window found in Figure 6.
Figure 6. Creating an About Dialog
Where to From Here?
Mono and Gtk# are both actively developed projects boasting an enormous amount of documentation, making your investigatory work all the easier. Hopefully this short tutorial gave you some insight into how to get started!
If you want to keep tabs on my progress with RadioRadio, follow me on GitHub, as I’ll be posting the files soon.