public CFNotificationObserverToken AddObserver(string name, INativeObject objectToObserve, Action <string, NSDictionary> notificationHandler, CFNotificationSuspensionBehavior suspensionBehavior = CFNotificationSuspensionBehavior.DeliverImmediately) { if (darwinnc != null && darwinnc.Handle == Handle) { if (name == null) { throw new ArgumentNullException("name", "When using the Darwin Notification Center, the value passed must not be null"); } } var strHandle = name == null ? IntPtr.Zero : NSString.CreateNative(name); name = name ?? NullNotificationName; var token = new CFNotificationObserverToken() { stringName = name, centerHandle = handle, nameHandle = strHandle, observedObject = objectToObserve == null ? IntPtr.Zero : objectToObserve.Handle, listener = notificationHandler }; // // To allow callbacks to add observers, we duplicate the list of listeners on AddObserver // We do the duplication on AddObserver, instead of making a copy on the notification // callback, as we expect the notification callback to be a more common operation // than the AddObserver operation // List <CFNotificationObserverToken> listenersForName; lock (listeners){ if (!listeners.TryGetValue(name, out listenersForName)) { listenersForName = new List <CFNotificationObserverToken> (1); CFNotificationCenterAddObserver(center: handle, observer: handle, callback: NotificationCallback, name: strHandle, obj: token.observedObject, suspensionBehavior: (IntPtr)suspensionBehavior); } else { listenersForName = new List <CFNotificationObserverToken> (listenersForName); } listenersForName.Add(token); listeners [name] = listenersForName; } return(token); }
public void AddObserver(string value, CFNotificationSuspensionBehavior suspensionBehavior = CFNotificationSuspensionBehavior.DeliverImmediately) { ObserverCount++; centers [handle] = this; var strHandle = value == null ? IntPtr.Zero : NSString.CreateNative(value); AddObserver(center: handle, observer: handle, callback: NotificationCallback, name: strHandle, obj: IntPtr.Zero, suspensionBehavior: suspensionBehavior); if (value != null) { NSString.ReleaseNative(strHandle); } }
public void AddObserver(string value, CFNotificationSuspensionBehavior suspensionBehavior = CFNotificationSuspensionBehavior.DeliverImmediately) { ObserverCount++; centers [handle] = this; var strHandle = value == null ? IntPtr.Zero : NSString.CreateNative (value); AddObserver (center: handle, observer: handle, callback: NotificationCallback, name: strHandle, obj: IntPtr.Zero, suspensionBehavior: suspensionBehavior); if (value != null) NSString.ReleaseNative (strHandle); }
static unsafe extern void AddObserver(CFNotificationCenterRef center, IntPtr observer, CFNotificationCallback callback, IntPtr name, IntPtr obj, CFNotificationSuspensionBehavior suspensionBehavior);
static extern unsafe void AddObserver(CFNotificationCenterRef center, IntPtr observer, CFNotificationCallback callback, IntPtr name, IntPtr obj, CFNotificationSuspensionBehavior suspensionBehavior);
public CFNotificationObserverToken AddObserver(string name, INativeObject objectToObserve, Action<string,NSDictionary> notificationHandler, CFNotificationSuspensionBehavior suspensionBehavior = CFNotificationSuspensionBehavior.DeliverImmediately) { if (darwinnc != null && darwinnc.Handle == Handle){ if (name == null) throw new ArgumentNullException ("name", "When using the Darwin Notification Center, the value passed must not be null"); } var strHandle = name == null ? IntPtr.Zero : NSString.CreateNative (name); var token = new CFNotificationObserverToken () { stringName = name, centerHandle = handle, nameHandle = strHandle, observedObject = objectToObserve == null ? IntPtr.Zero : objectToObserve.Handle, listener = notificationHandler }; // // To allow callbacks to add observers, we duplicate the list of listeners on AddObserver // We do the duplication on AddObserver, instead of making a copy on the notification // callback, as we expect the notification callback to be a more common operation // than the AddObserver operation // List<CFNotificationObserverToken> listenersForName; lock (listeners){ if (!listeners.TryGetValue (name, out listenersForName)){ listenersForName = new List<CFNotificationObserverToken> (1); CFNotificationCenterAddObserver (center: handle, observer: handle, callback: NotificationCallback, name: strHandle, obj: token.observedObject, suspensionBehavior: (IntPtr) suspensionBehavior); } else listenersForName = new List<CFNotificationObserverToken> (listenersForName); listenersForName.Add (token); listeners [name] = listenersForName; } return token; }