Using Key-Value Coding with MonoMac

Key-Value Coding is a set of practices that allow applications to access object properties using strings.  This is similar to Binding Expressions in Silverlight.   In both cases the purpose is to allow tooling that does not directly have access to your native code to access properties from your program.

You would typically use this from a tool like Interface Builder, where you can use the Binding Inspector (⌘4 from Interface Builder) to explore the possible values of the component that you can data bind.   In Interface Builder you would use string to describe the name of the property that you want to access.   For example, consider the options available for the NSPopUpButton component show on the right.   All of those properties of the NSPopUpButton can be pulled directly from your code from Interface Builder without having to manually hook things up.

To use this with MonoMac, all you have to do is make sure that any property that you want to access from Interface Builder is flagged with the [Export] attribute.   If you apply the Export attribute without any parameters, it will expose the C# name of your property for Key-Value access.    You can change the name that is exposed by passing a parameter, for example: [Export (“myIndex”)].

The QTRecorder sample shows how various elements in the UI are connected to the code:

MonoMac port of QTRecorder sample

In that sample, the class QTRecorder, a class derived from NSDocument exposes properties that are hooked up directly to the Enabled properties in various buttons in the UI and properties to populate and control the contents of the various popups in the application.   All of these bindings are configured by setting the target of the binding to be the “File’s Owner”, in this case the QTRecorder class, as it happens to load the QTRecorder.nib file.    You can see the complete list of bindings in the screenshot on the left.

Let us explore what happens in the application.   The Video Devices popup is controlled through three bindings: the Content, the Content Values and the Selected Object.    This is what the binding are configured to in Interface Builder for all three properties:

The Content is bound to the VideoDevices property in the source code, this is a property that merely returns the strongly typed QTCaptureDevice array of the available devices.   The Content Values is bound to the VideoDevices.localizedDisplayName.   The first part of the key matches our VideDevices array, and the second part of the key happens to be the Objective-C name of a property exposed by the QTCaptureDevice that provides the name of the device to display.    The actual item selected on the screen is controlled by the Selected Object binding.   In this case, a new property, the SelectedVideoDevice property.   At this point, the NSPopUpButton will populate its contents using these bindings.   Additionally, when the user selects a different item, the property bound to the Selected Object will be invoked with the new value.   Our code responds by configuring the QTCaptureSession accordingly.

Another interesting binding takes place with the recording button.   The recording button hasits Enabled property bound to the HasRecordingDevice C# property and has its Value bound to the C# Recording property.   When we change the video source in the popup, the button responds by either getting grayed out or becoming active.   Although this could have been done programmatically in response to the new value set in the SelectedVideoDevice property, the code takes advantage of the built-in notification system and instead reacts to changes to the SelectedVideoDevice automatically.

This is done by using the dependency keys feature a feature of Key-Value Coding. It requires that the code exports a specially-named method which is prefixed with the string “keyPathsForValuesAffecting”.   These methods are are meant to return an NSSet containing the names of the properties that depend on that particular property.   In our case this is:

public static NSSet keyPathsForValuesAffectingHasRecordingDevice ()
    return new NSSet ("SelectedVideoDevice", "SelectedAudioDevice");

Once that is done, the runtime knows that when either one of the SelectedVideoDevice or SelectedAudioDevice change, it has to query the value for HasRecordingDevice again.

This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Using Key-Value Coding with MonoMac

  1. eric says:

    Could you add the blog to to help promote MonoMac among the mac developers? (send an email with details to to join)

  2. Hi Miguel,

    I downloaded this sampe (QTRecorder) from MonoMac Github and try to run in on MonoDeveloper and i’m getting an error related to the CIFilter class. Is it already implemented/binded correctly?

    And one side question is it available on MonoMac an easy method to convert from a NSImage to a System.Drawing.Bitmap?

    Thanks in advance,


    • Hello Sergio,

      My apologies, the version on the samples/ module has been update to use new features available only if you build MonoMac from the source code.

      Just make sure you are using the monomac-0-4 tag (do a git pull before):

      git checkout monomac-0-4

      That will give you the samples in the state they were before work continued to MonoMac

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s