편집

다음을 통해 공유


NSObject.AddObserver Method

Definition

Overloads

AddObserver(NSString, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

AddObserver(String, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

AddObserver(NSObject, NSString, NSKeyValueObservingOptions, IntPtr)

Registers an object for being observed externally (using NSString keyPath).   Observed changes are dispatched to the observer’s object ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.

AddObserver(NSObject, String, NSKeyValueObservingOptions, IntPtr)

Registers an object for being observed externally (using string keyPath).   Observed changes are dispatched to the observer’s object ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.

AddObserver(NSString, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

public IDisposable AddObserver (Foundation.NSString key, Foundation.NSKeyValueObservingOptions options, Action<Foundation.NSObservedChange> observer);
member this.AddObserver : Foundation.NSString * Foundation.NSKeyValueObservingOptions * Action<Foundation.NSObservedChange> -> IDisposable

Parameters

key
NSString

Key-path to use to perform the value lookup. The keypath consists of a series of lowercase ASCII-strings with no spaces in them separated by dot characters.

options
NSKeyValueObservingOptions

Flags indicating which notifications you are interested in receiving (New, Old, Initial, Prior).

observer
Action<NSObservedChange>

Method that will receive the observed changes.   It will receive a NSObservedChange parameter with the information that was changed.

Returns

An IDisposable object.  Invoke the Dispose method on this object to remove the observer.

Remarks

When the object is registered for observation, changes to the object specified in the keyPath that match the flags requested in options will be sent to the specied method (a lambda or method that matches the signature).

This version provides the convenience of exposing the changes as part of the strongly typed NSObservedChange object that is received by the target.

void Setup ()
{
    AddObserver (rateKey, NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, (observed) => {
        Console.WriteLine ("Change: {0}", observed.Change);
        Console.WriteLine ("NewValue: {0}", observed.NewValue);
        Console.WriteLine ("OldValue: {0}", observed.OldValue);
        Console.WriteLine ("Indexes: {0}", observed.Indexes);
        Console.WriteLine ("IsPrior: {0}", observed.IsPrior);
    });
}

Applies to

AddObserver(String, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

public IDisposable AddObserver (string key, Foundation.NSKeyValueObservingOptions options, Action<Foundation.NSObservedChange> observer);
member this.AddObserver : string * Foundation.NSKeyValueObservingOptions * Action<Foundation.NSObservedChange> -> IDisposable

Parameters

key
String

Key-path to use to perform the value lookup. The keypath consists of a series of lowercase ASCII-strings with no spaces in them separated by dot characters.

options
NSKeyValueObservingOptions

Flags indicating which notifications you are interested in receiving (New, Old, Initial, Prior).

observer
Action<NSObservedChange>

Method that will receive the observed changes.   It will receive a NSObservedChange parameter with the information that was changed.

Returns

An IDisposable object.  Invoke the Dispose method on this object to remove the observer.

Remarks

When the object is registered for observation, changes to the object specified in the keyPath that match the flags requested in options will be sent to the specied method (a lambda or method that matches the signature).

This version provides the convenience of exposing the changes as part of the strongly typed NSObservedChange object that is received by the target.

void Setup ()
{
    AddObserver (rateKey, NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, (observed) => {
        Console.WriteLine ("Change: {0}", observed.Change);
        Console.WriteLine ("NewValue: {0}", observed.NewValue);
        Console.WriteLine ("OldValue: {0}", observed.OldValue);
        Console.WriteLine ("Indexes: {0}", observed.Indexes);
        Console.WriteLine ("IsPrior: {0}", observed.IsPrior);
    });
}

Applies to

AddObserver(NSObject, NSString, NSKeyValueObservingOptions, IntPtr)

Registers an object for being observed externally (using NSString keyPath).   Observed changes are dispatched to the observer’s object ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.

[Foundation.Export("addObserver:forKeyPath:options:context:")]
public virtual void AddObserver (Foundation.NSObject observer, Foundation.NSString keyPath, Foundation.NSKeyValueObservingOptions options, IntPtr context);
abstract member AddObserver : Foundation.NSObject * Foundation.NSString * Foundation.NSKeyValueObservingOptions * nativeint -> unit
override this.AddObserver : Foundation.NSObject * Foundation.NSString * Foundation.NSKeyValueObservingOptions * nativeint -> unit

Parameters

observer
NSObject

The object that will receive the notifications

keyPath
NSString

Key-path to use to perform the value lookup. The keypath consists of a series of lowercase ASCII-strings with no spaces in them separated by dot characters.

options
NSKeyValueObservingOptions

Flags indicating which notifications you are interested in receiving (New, Old, Initial, Prior).Context data passed to the ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method when one of the properties change.

context
IntPtr

nativeint

Opaque constant that will be passed to the ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method

Attributes

Remarks

When the object is registered for observation, changes to the object specified in the keyPath that match the flags requested in options will be sent to the ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method in the observer object.

To stop receiving notifications, call the RemoveObserver(NSObject, String, IntPtr) method.

class MySampleViewController : UIViewController {
    // A token to tell different observed properties appart.
    const IntPtr tokenObserveRate = (IntPtr) 1;
    const IntPtr tokenObserveVolume = (IntPtr) 2;

    void Setup ()
    {

        Player.AddObserver (this, (NSString)"rate",
            NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New,
            tokenObserveRate);
        Player.AddObserver (this, (NSString)"volume",
            NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New,
            tokenObserveVolume);

    }
    public override void ObserveValue (NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr ctx)
    {
        // Handle change.
        if (ctx == tokenObserveRate) {
            // the rate changed
        } else if (ctx == tokenObserveVolume){
            // the volume changed
        } else {
            // invoke the base implementation for unhandled events
            base.ObserveValue (keyPath, ofObject, change, ctx);
        }
    }
}

Applies to

AddObserver(NSObject, String, NSKeyValueObservingOptions, IntPtr)

Registers an object for being observed externally (using string keyPath).   Observed changes are dispatched to the observer’s object ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.

public void AddObserver (Foundation.NSObject observer, string keyPath, Foundation.NSKeyValueObservingOptions options, IntPtr context);
member this.AddObserver : Foundation.NSObject * string * Foundation.NSKeyValueObservingOptions * nativeint -> unit

Parameters

observer
NSObject

The object that will receive the notifications.   The object will receive notifications on its ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.

keyPath
String

Key-path to use to perform the value lookup. The keypath consists of a series of lowercase ASCII-strings with no spaces in them separated by dot characters.

options
NSKeyValueObservingOptions

Flags indicating which notifications you are interested in receiving (New, Old, Initial, Prior).

context
IntPtr

nativeint

Context data passed to the ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method when one of the properties change.

Remarks

When the object is registered for observation, changes to the object specified in the keyPath that match the flags requested in options will be sent to the ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method in the observer object.

To stop receiving notifications, call the RemoveObserver(NSObject, String, IntPtr) method.

class MySampleViewController : UIViewController {
    void Setup ()
    {
        Player.AddObserver (this, (NSString)"rate",
            NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New,
            IntPtr.Zero);
    }
    public override void ObserveValue (NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr ctx)
    {
        // Handle change.
    }
}

Applies to