Skip to content

Instantly share code, notes, and snippets.

@abock
Created October 10, 2016 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abock/c14226892f2f808a14c4265dfc07bbe9 to your computer and use it in GitHub Desktop.
Save abock/c14226892f2f808a14c4265dfc07bbe9 to your computer and use it in GitHub Desktop.
//
// NSKeyValueBinding.cs
//
// Author:
// Aaron Bockover <abock@xamarin.com>
//
// Copyright 2016 Microsoft. All rights reserved.
using System;
using System.Runtime.InteropServices;
using Foundation;
using ObjCRuntime;
namespace AppKit
{
static class NSKeyValueBinding
{
[DllImport (Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]
static extern void void_objc_msgSend_IntPtr_IntPtr_IntPtr_IntPtr (
IntPtr handle, IntPtr sel, IntPtr binding, IntPtr observable, IntPtr keyPath, IntPtr options);
static readonly IntPtr bindToObjectWithKeyPathOptionsSel = Selector.GetHandle (
"bind:toObject:withKeyPath:options:");
public static void Bind (
this NSObject obj,
NSString binding,
NSObject observable,
string keyPath,
NSDictionary options = null)
{
if (obj == null)
throw new ArgumentNullException (nameof (obj));
if (binding == null)
throw new ArgumentNullException (nameof (binding));
if (observable == null)
throw new ArgumentNullException (nameof (observable));
if (keyPath == null)
throw new ArgumentNullException (nameof (keyPath));
var keyPathNative = NSString.CreateNative (keyPath);
try {
void_objc_msgSend_IntPtr_IntPtr_IntPtr_IntPtr (
obj.Handle,
bindToObjectWithKeyPathOptionsSel,
binding.Handle,
observable.Handle,
keyPathNative,
options == null ? IntPtr.Zero : options.Handle);
} finally {
NSString.ReleaseNative (keyPathNative);
}
}
public static readonly NSString SelectedIndexBinding = Dlfcn.GetStringConstant (
Dlfcn.dlopen (Constants.AppKitLibrary, 0),
"NSSelectedIndexBinding");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment