UIDevice.Notifications.ObserveBatteryStateDidChange Method

Definition

Overloads

ObserveBatteryStateDidChange(EventHandler<NSNotificationEventArgs>)

Strongly typed notification for the BatteryStateDidChangeNotification constant.

ObserveBatteryStateDidChange(NSObject, EventHandler<NSNotificationEventArgs>)

Strongly typed notification for the BatteryStateDidChangeNotification constant.

ObserveBatteryStateDidChange(EventHandler<NSNotificationEventArgs>)

Strongly typed notification for the BatteryStateDidChangeNotification constant.

public static Foundation.NSObject ObserveBatteryStateDidChange (EventHandler<Foundation.NSNotificationEventArgs> handler);
static member ObserveBatteryStateDidChange : EventHandler<Foundation.NSNotificationEventArgs> -> Foundation.NSObject

Parameters

handler
EventHandler<NSNotificationEventArgs>

Method to invoke when the notification is posted.

Returns

Token object that can be used to stop receiving notifications by either disposing it or passing it to RemoveObservers(IEnumerable<NSObject>)

Remarks

The following example shows how you can use this method in your code

//
// Lambda style
//

// listening
notification = UIDevice.Notifications.ObserveBatteryStateDidChange ((sender, args) => {
    /* Access strongly typed args */
    Console.WriteLine ("Notification: {0}", args.Notification);
});

// To stop listening:
notification.Dispose ();

//
//Method style
//
NSObject notification;
void Callback (object sender, Foundation.NSNotificationEventArgs args)
{
    // Access strongly typed args
    Console.WriteLine ("Notification: {0}", args.Notification);
}

void Setup ()
{
    notification = UIDevice.Notifications.ObserveBatteryStateDidChange (Callback);
}

void Teardown ()
{
    notification.Dispose ();
}

Applies to

ObserveBatteryStateDidChange(NSObject, EventHandler<NSNotificationEventArgs>)

Strongly typed notification for the BatteryStateDidChangeNotification constant.

public static Foundation.NSObject ObserveBatteryStateDidChange (Foundation.NSObject objectToObserve, EventHandler<Foundation.NSNotificationEventArgs> handler);
static member ObserveBatteryStateDidChange : Foundation.NSObject * EventHandler<Foundation.NSNotificationEventArgs> -> Foundation.NSObject

Parameters

objectToObserve
NSObject

The object to observe.

handler
EventHandler<NSNotificationEventArgs>

Method to invoke when the notification is posted.

Returns

Token object that can be used to stop receiving notifications by either disposing it or passing it to RemoveObservers(IEnumerable<NSObject>)

Remarks

This method can be used to subscribe for BatteryStateDidChangeNotification notifications.

// Listen to all notifications posted for any object
var token = UIDevice.Notifications.ObserveBatteryStateDidChange ((notification) => {
	Console.WriteLine ("Observed BatteryStateDidChangeNotification!");
};

// Listen to all notifications posted for a single object
var token = UIDevice.Notifications.ObserveBatteryStateDidChange (objectToObserve, (notification) => {
	Console.WriteLine ($"Observed BatteryStateDidChangeNotification for {nameof (objectToObserve)}!");
};

// Stop listening for notifications
token.Dispose ();

Applies to