編輯

共用方式為


UIDevice.BatteryStateDidChangeNotification Property

Definition

Notification constant for BatteryStateDidChange

[Foundation.Advice("Use UIDevice.Notifications.ObserveBatteryStateDidChange helper method instead.")]
[Foundation.Field("UIDeviceBatteryStateDidChangeNotification", "UIKit")]
[ObjCRuntime.Unavailable(ObjCRuntime.PlatformName.TvOS, ObjCRuntime.PlatformArchitecture.All, null)]
public static Foundation.NSString BatteryStateDidChangeNotification { [ObjCRuntime.Unavailable(ObjCRuntime.PlatformName.TvOS, ObjCRuntime.PlatformArchitecture.All, null)] get; }
member this.BatteryStateDidChangeNotification : Foundation.NSString

Property Value

NSString constant, should be used as a token to NSNotificationCenter.

Attributes

Remarks

This constant can be used with the NSNotificationCenter to register a listener for this notification. This is an NSString instead of a string, because these values can be used as tokens in some native libraries instead of being used purely for their actual string content. The 'notification' parameter to the callback contains extra information that is specific to the notification type.

If you want to subscribe to this notification, you can use the convenience UIDevice.Notifications.ObserveBatteryStateDidChange method which offers strongly typed access to the parameters of the notification.

The following example shows how to use the strongly typed Notifications class, to take the guesswork out of the available properties in the notification:

//
// 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 ();
}

The following example shows how to use the notification with the DefaultCenter API:

// Lambda style
NSNotificationCenter.DefaultCenter.AddObserver (
        UIDevice.BatteryStateDidChangeNotification, (notification) => {Console.WriteLine ("Received the notification UIDevice", notification); }


// Method style
void Callback (NSNotification notification)
{
    Console.WriteLine ("Received a notification UIDevice", notification);
}

void Setup ()
{
    NSNotificationCenter.DefaultCenter.AddObserver (UIDevice.BatteryStateDidChangeNotification, Callback);
}

This can be used from a background thread.

Applies to