Friday, April 10, 2009

Write .NET applications on Mono and use them in Windows

I am no professional .NET developer but I thought I would have a crack at a simple application written on my Fedora 10 in the Monodevelop IDE and then run the same exe it compiles in a Windows XP. It took a while but I got there in the end and in retrospect it was not too hard. Just a few dependancies, a short forum-search for a solution to a small problem.

Here is what I did:

1. Install mono, monodevelop and gtk-sharp ( GTK# ):
The monodevelop IDE provides a gui designer for gtk#. So I thought I would try this out and see how I could get that working in windows too. In Fedora I just ran the following:

# yum install monodevelop gtk-sharp*

As usual I let yum decide dependancies for me.

2. Start monodevelop and create a new solution:

Select the C# templateand then pick Gtk# 2.0 Project
Under Solution name: type "tutorial" and under Name: type "myGuiApp" to create a Gtk# GUI application under the folder {$HOME}/Projects/tutorial.

Click Forward and select your Target GTK environment and click Finish.

There will be two files in your solution: Main.cs and MainWindow.cs. You will only need to work on MainWindow.cs

3. Design your application in Monodevelop:

This will be a very simple application. Only a little more advanced than Hello, World.
Double click on MainWindow.cs to start editing the Window. Monodevelop has already provided you with a basic GUI shell to work with.
Beneath the source code window on MainWindow.cs you will see two buttons: Source Code and Designer. Click the Designer button to start dragging and dropping your tools onto your palette.

Seeing as though you are using Gtk# to create this GUI there is something that you should know before continuing. It requires the use of containers to handle the positioning of widgets. For the purposes of this tutorial I will just use the Fixed Container.

Drag a Fixed container onto your pallet first. Then drag the following widgets onto the pallet. Position them something like the next screen shot.

3 Labels
2 buttons
1 entry box

Resize your pallet to suit.

Above the designer pallet you will see a dropdown list that can be used to select each element on your pallet starting with the window itself. Select each of these and then adjust their properties as below:

MainWindow: Window Properties: Window Title = "My Gui App"
label1: Label Properties: Label = "Welcome to My GUI APP"
label2: Label Properties: Label = "Your name please:"
label3: Label Properties: Label = "Hi there, "
button1: Button Properties: Label = "Greet!"
button2: Button Properties: Label = "Close"

You may have to rearrange things a bit to get everything fitting on your screen. Now it should look something like this:

4. Add signal handlers to the buttons

Click button1 ( now labelled, "Greet!" ) and under the properties pallet select the Signals tab. Next under the Button Signals double click on the Clicked signal. Monodevelop will automatically create a handler called OnButton1Clicked for you.

Click button2 ( now labelled, "Close" ) and do the same as you did for button1.

5. Time to start editing some code:

Click the Source Code button to view the source code for MainWindow.cs. You will find two new functions called:

protected virtual void OnButton1Clicked (object sender, System.EventArgs e)

protected virtual void OnButton2Clicked (object sender, System.EventArgs e)

Edit them to look like this:

protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
label3.Text = label3.Text + entry1.Text;

protected virtual void OnButton2Clicked (object sender, System.EventArgs e)

6. Compile:

Click the build button and try your application out. It should compile without any difficulties at all. Should being the operative word. If your app did not compile do not proceed. Get it working first!

7. Getting Windows XP ready:

Getting Windows ready was as simple as installing the Mono package for windows. It includes a command prompt mode. Also you can create shortcuts to your mono apps that execute the mono launcher to launch your code. A bit like the java stuff.

I have yet to get it to work effectively on both linux and windows without having to first install the mono environment on both. I had thought that mono applications written on Linux could be migrated over to Windows without the mono libraries present.

I have yet to test how a windows app written for .NET v2 will port over to Linux. Again I think that without winforms it will not work. It might work for a command prompt application. But that's about it.

Anway here it is working just fine in my qemu windows xp vm.

No comments: