Building Mac Applications in MonoDevelop with MonoMac

Recently Miguel announced MonoMac, a new Mono binding for Cocoa & other Objective-C libraries, based on the MonoTouch binding generator and Objective-C bridge.

This is exciting for many people because it allows writing native Mac GUIs using C# and other .NET languages. I have put together a MonoDevelop addin that simplifies the process of creating, developing and debugging a MonoMac application.

I have created a brief walkthrough to show how easy it is to create a simple Mac application with MonoMac, MonoDevelop and Interface Builder.

First, create a new MonoMac C# project using the New Project dialog.

MonoDevelop New Project dialog showing MonoMac Project types

The new project has an entry point, a definition of the main menu and app delegate, and a window with a controller. The xib files are interface definitions that can be edited with Apple's Interface Builder GUI designer tool. The xib.designer.cs files contain autogenerated partial classes for any classes, actions and outlets defined in the xib files. The Info.plist file is an application manifest, and MonoDevelop will automatically merge in some required values when building the app bundle.

A newly-created MonoMac project in MonoDevelop

The MainWindow.xib file can be opened in Interface Builder.

Interface Builder with the MonoMac project's main window

After adding an NSButton and NSTextField to the window, add an outlet of type NSTextField to the controller definition in the Library.

Adding an outlet to the MainWindowController

Connect this outlet to the text field on the window by dragging the outlet from the Inspector.

Connecting the text field to the outlet on the controller

Similarly, create an action of type NSButton, and connect it to the button on the window.

Connecting the button to an action on the controller

MonoDevelop automatically updates the designer code when it regains focus. The designer partial class contains the new action and outlet.

The autogenerated designer code for the controller's outlets and actions

Implement the action in the controller class. MonoDevelop will automatically complete the partial method signature.

Implementation of the action in the controller

The application is compiled to an app bundle, which can be run or debugged like any other Mono application.

Debugging the MonoMac application

Note that this is an experimental preview, the result of a few days' work, and MonoMac itself is also in an immature state. I make no guarantee about stability of the MonoMac APIs, the MonoDevelop CodeBehind generator, the project format, or functionality of any of the usual MonoDevelop features. With these caveats, you can download a MonoDevelop build with MonoMac included.

UPDATE: The addin can now be installed into the latest version of MonoDevelop 2.4 by following these instructions.

This is a community effort, not a commercial product, so we need your help improving both MonoMac and the MonoDevelop addin!

  • Binding and auditing more Framework APIs.
  • Writing samples and templates to set the patterns for MonoMac applications.
  • Writing tutorials and documentation.
  • Porting Cocoa samples to exercise the bindings and tooling, and prioritize API binding.
  • Enabling the MonoDevelop addin to expose existing C# classes and project resources to Interface Builder.

Email the mono-osx mailing list to join in!

Comments

I already have version 2.2 of monodevelop in my applications folder. If I download this experimental version into another folder should it run ok ? I tried that and I couldn't find any sign of the monomac items in the new solutions dialog.

I accidentally uploaded a build that didn't contain the new addin. I've fixed this now.

You can have as many MD versions on your system as you want, but be careful not to confuse them, as the app bundles all have the same name. Do not run different versions concurrently, and beware also that each time you switch versions all the code completion databases will be regenerated fully, and MD may silently fail to start the first time you run the new version.

Thanks, its working now. Its very impressive, like monotouch, it just works.

I couldn't find any sign of the monomac items in the new solutions dialog either.

OK. Downloaded the updated version. Everything's fine now.

Very excited to get started.

I downloaded the latest stable MonoDevelop, stable version. I can see the add-in in the add-in manager, but I don´t see the "MonoMac" option when creating a new Solution. After an upgrade auto-detected by MonoDevelop, still nothing.
I downloaded the Beta version, 2.4.1. Still, can´t see the MonoMac option.
Any hint on this?
Thanks!

Hard to answer without more info. I suggest you mail the mailing list - it'll be easier to keep track there.

How large are the app bundles produced by MonoMac?

The app bundle I made with the above walkthrough is 1.7 MB uncompressed, of which 1.6 MB is MonoMac.dll. The other 0.1 MB is the actual executable, debug symbols, launch script, and nib files.

The bundle depends on Mono.framework - the launch script checks for it and will prompt the user to install it - but MonoMac.dll currently isn't ready to be included in Mono.framework, so we include it in the bundle. In the future, when MonoMac is more mature, we will most likely include MonoMac.dll in Mono.framework, so it doesn't have to be included in the app bundle - i.e. the bundle from this example would be 0.1MB uncompressed. However, your users will have to install the 60MB Mono.framework package.

It would be possible to include Mono in the app bundle, thus removing the Mono.framework dependency. By using the MonoTouch linker to eliminate unused code, it should be possible to get it down to a few megabytes. However, this isn't something we're currently working on.

Wow, wow, wow.

I'm testing it now and works like a charm. I'm planning to write some examples (so I can learn too) and try to help with this
Full contained bundle generation would be a great improvement too :)

Thanks for a great work

Doesnt understand c# named args and optional parameters. Is there a timeline when this will be available

I wrote a more detailed version of your article. When I went through this one some of the Interface Builder items where not that intuitive at least for me. Hopefully it will help folks. The link is http://www.satter.org/2010/09/net-development-for-os-x.html

I get the dreaded "ibtool not found. Please ensure the Apple SDK is installed." error upon trying to compile with XCode 3.2.5 installed. Should I really downgrade to XCode 3.2.4 as suggested on StackOverflow? Is MonoMac compatible with 3.2.5?

I have xcode 3.2.5 and ibtool works fine. I guess there is a bug in the Apple installer, or maybe you installed it to a different drive and it somehow isn't in PATH.

No longer getting that "ibtool not found" message, but I am getting a "Error CS0759: A partial method `MonoMacTest.MainWindowController.ButtonAction(MonoMac.Foundation.NSObject)' implementation is missing a partial method declaration" message when "partial void ButtonAction (MonoMac.AppKit.NSButton sender);" is clearly defined in my MainWindow.xib.designer.cs file. Is that other file not being compiled or linked or what? Dunno.

MonoMac.Foundation.NSObject != MonoMac.AppKit.NSButton

Also, my blog is not a support forum. Please use the MonoMac mailing list, or StackOverflow.com.

It is in the message log:
08:03:30 - /Library/Frameworks/Mono.framework/Versions/2.6.7/bin/gmcs "/out:/Users/flipdoubt/Projects/MonoMacTest/MonoMacTest/bin/Debug/MonoMacTest.exe"
"/r:/Library/Frameworks/Mono.framework/Versions/2.6.7/lib/mono/2.0/System.dll"
"/r:/Library/Frameworks/Mono.framework/Versions/2.6.7/lib/mono/2.0/System.Xml.dll"
"/r:/Library/Frameworks/Mono.framework/Versions/2.6.7/lib/mono/2.0/System.Core.dll"
"/r:/Library/Frameworks/Mono.framework/Versions/2.6.7/lib/mono/2.0/System.Xml.Linq.dll"
"/r:/Library/Frameworks/Mono.framework/Versions/2.6.7/lib/mono/2.0/System.Drawing.dll"
"/r:/Users/flipdoubt/.config/MonoDevelop/addins/MonoDevelop.MonoMac.2.4.0.12/MonoMac.dll"
/nologo
/warn:4
/debug:+
/debug:full
/optimize-
/codepage:utf8
"/define:DEBUG"

/t:exe
"/Users/flipdoubt/Projects/MonoMacTest/MonoMacTest/MainWindow.cs"
"/Users/flipdoubt/Projects/MonoMacTest/MonoMacTest/MainWindowController.cs"
"/Users/flipdoubt/Projects/MonoMacTest/MonoMacTest/MainWindow.xib.designer.cs"
"/Users/flipdoubt/Projects/MonoMacTest/MonoMacTest/Main.cs"
"/Users/flipdoubt/Projects/MonoMacTest/MonoMacTest/AppDelegate.cs"
"/Users/flipdoubt/Projects/MonoMacTest/MonoMacTest/MainMenu.xib.designer.cs"

Hello,

I installed monodevelop mac version, when i develop the c# TeeIter i can't found the MonoMac subitems and templates.
The monodevelop version is :
Release ID: 20401003
Git revision: 2f1746af36f421d262dcd2b0542ce86b12158f02

Sincerly,

Did you install the addin from the link at the bottom of the post?

i followed instructions up to "Connect this outlet to the text field on the window by dragging the outlet from the Inspector" but when i try to drag/drop outlet to the NSTextField on the Window, it does nothing.
Can't figure out what i am doing wrong.

Xcode 4 changes how outlets and actions are connected, and MonoDevelop's integration changed substantially to support it. For more information, see the Xamarin docs - MonoTouch and MonoMac are very similar in this regard.

i have been searching and searching for help on this.
can anyone point me to any good web sites or books that deal primarily w/ coding w/ MonoDevelop, C#, & XCode for Mac?