Skip to content

Instantly share code, notes, and snippets.

@abock abock/NSKeyValueBinding.cs Secret
Created Oct 10, 2016

Embed
What would you like to do?
//
// 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
You can’t perform that action at this time.