Yes you read that right. This entry will show you the basics of building a Mac application in .NET. How can this be .NET is a Microsoft Windows technology? Enter Mono. And no I don't mean the disease. I mean the Mono Project a Novell project that has ported .NET to Linux and other platforms including Mac OS X. What follows is a step by step guide to using .NET and no PCs were killed in the writing of this blog entry.
Get a Mac
Step 1 get a Mac. Prices can actually be very reasonable for a Apple Mac Mini a low cost of $699. I decide to shutdown my PC and just use the Mac Mini with my MS keyboard/mouse and Samsung PX2370 . Thus literally all I bought was the Mini. Now comes the hard part setting up.
Plug in the Mini to power. No stupid power brick, very nice. Next, I plugged in the speakers, network, mic and video. Finally, I turned on the power. I went through a couple of screens to setup my language, time, network, Apple Id and user id. All of this was quick and painless. While it could not recognize my keyboard, all I did was tell it ANSI and it was set. It even fired up iTunes when I hit my play key. Not surprising iTunes complained it was not the latest version and asked to update. Sure go ahead do your update.
Yeah if I was a consumer I would be done and ready to go. But I am a developer and had miles to go before I could program. I will not bore you with the details but give you a list of things to do.
- Go and signup to Apple's developer program. I selected both Mac and iOS development. Once I was signed up I downloaded Xcode including Mac development. This took a while as Xcode is huge like Visual Studio 2+ Gbs.
- Next I downloaded and installed MonoDevelop 2.4, an IDE from Mono. It also requires Mono 2.4 and GTK.
- Finally I downloaded and installed MonoMac a new community project building a Mac UI bindings for Mono. I downloaded and installed the latest trunk build.
It took a while but soon I had Mono Developer up and running.
Building HelloMac
If you have not already fire up MonoDevelop. I am going to walk you step by step in building your first hello world application. It is based on the blog entry by Michael Hutchinson. His article is ok but shy on details and steps. It took me some experimenting to get my application to work.
First you need to create a project. From the Welcome screen or File Menu select New Solution. I am using C#. I opened up the C# node and selected MonoMac sub node. Then selected MonoMac Project and named my project HelloMonoMac.
This gave me a solution with a single project that contained
- info.plist - text file containing property strings about your application. See reference guide for more information.
- Main.cs - this is the code for a class with a Main method.
- MainMenu.xib - NIB fileset containing the menu resources and coding. See Interface Builder User Guide for more information.
- MainWindow.xib - NIB fileset for your main window. See Interface Builder User Guide for more information.
MonoMac tries to maintain the Xcode paradigm for building UI applications. Xcode uses a MVC model. The UI is contained in the NIB file and has extensions for your code.Your controller and model will be in .NET while the view will be written using Interface Builder hence why Xcode had to be installed.
Using Interface Builder the UI is laid out, extensions and action interfaces created. Thus our first step after creating the project is to open the MainWindow.xib and build the UI. Double click the MainWindow.xib node in the solution viewer.
In the layout you should see 4 Windows. The first Window, Library, on the Left would be like the Toolbox in VS. It contains widgets to drag and drop onto your Window. In the middle you should see two Windows. The top one contains a list of objects in the NIB. The Window canvas on which you will layout the Window is below. The last Window is a window similar to the Property Window in VS.
Look for NSButton in the Library. Drag the button on to the canvas. Change the title from "Button" to "Click Me!"
Next add a NSTextField with "Click the Button!" as the title.One of the nice things is you can click into the TextField and change the text.
The next step is to create an interface between the UI and our control. First is a property to update the TextField. Then an event that the control can receive when the button is clicked. Inside the event the controller written in c# will update the the string in the TextField using the property.
Goto the Library and look for MainWindowController and click on it. Below the list of widgets you should see MainWindowController with Inheritance in the selection. Change it from Inheritance to Outlets. Using the "+" button add a new outlet. Name the outlet label and the type should be NSTextField. To change the type click on "id".
The next step is to map our actual NSTextField to label. This is accomplished using the Toolbox and canvas. Bring up the MainWindow.xib Window, the top middle Window, and click "File's Owner". In the property Window you should see it say MainWindow.Control.Attributes in the caption. You might need to double click it to have the property window change. On the property window click the 5th button that looks like an arrow pointing to the right. This puts you into the connections settings. Under outlets you should see one called "label" with a circle on the right. When you hoover your mouse over the circle it changes into a "+" sign. Click and drag it to the NSTextField on the canvas. When you release the mouse you should see your NSTextField linked as the label.
Almost done. The next step is in Interface Builder to setup an event for when the button is clicked. For this an action in the interface must be created. It will link the action to the button. Go to the Library window. Make sure it is still showing the outlets for the MainWindowController. Change to Actions. Click the "+" and add buttonClicked as a NSButton. In the property window you should see a new received action called buttonClicked. Link it to the button on the canvas the same way you did the label.
The last step is to save your changes. This is important as MonoDevelop needs to read in the changes. In building my first application I forgot to save. When I ran the program I got a null reference exception until I saved the file. You can save the file using the menu.
Now it is time to switch back to MonoDevelop and write the one bit of code, buttonClicked event. Bring MonoDevelop back up. And add the code below into the MainWindow.Controller.cs file. The key is creating a method called buttonClicked that takes a NSButton. The application will call this method whenever the button is clicked. Inside the buttonClicked method the label linked to the NSTextField is modified. Compiler and run.
int count = 0; partial void buttonClicked(NSButton sender) { label.StringValue = string.Format("Button clicked {0} times.", ++count); }
I hope this helps you with your .NET programming with MonoMac. Good luck.
Hi, I've tried to download a MonoMac, but link in 3rd step links only to MonoDevelop, which I already have. Am I doing something wrong? On the other side, thanx for great article!
Posted by: Tomas Vymazal | September 23, 2010 at 03:24 AM
The trunk build is a version of MonoDevelop that includes MonoMac. My bad for not being more explicit on the directions.
Posted by: Rabi Satter | September 23, 2010 at 10:29 AM