While DeviceStatus
allows you to retrieve device information, it also includes the following two events:
KeyboardDeployedChanged
PowerSourceChanged
If the phone device has a hardware keyboard, such as a sliding keyboard, the KeyboardDeployedChanged
event allows you to detect when the keyboard is extended.
You can subscribe to the KeyboardDeployedChanged
event as shown:
DeviceStatus.KeyboardDeployedChanged += HandleKeyboardDeployedChanged;
The event handler can be used to determine whether the keyboard is deployed using the DeviceStatus
class, as shown:
void HandleKeyboardDeployedChanged(object sender, EventArgs e)
{
bool keyboardDeployed = DeviceStatus.IsKeyboardDeployed;
...
}
When the phone device is connected to a user’s computer, it may be a good time to perform some processor-intensive task that could potentially consume a lot of power, which would otherwise flatten the user’s battery. The PowerSourceChangedEvent
allows you to detect when the user attaches or detaches an external power supply.
The PowerSourceChanged
event can be subscribed to as shown:
DeviceStatus.PowerSourceChanged += HandlePowerSourceChanged;
The event handler can be used to retrieve the new PowerSource
value from the DeviceStatus
class, as shown:
void HandlePowerSourceChanged(object sender, EventArgs e)
{
PowerSource powerSource = DeviceStatus.PowerSource;
...
}
Note
The DeviceState.PowerSourceChanged
event is not raised on the app’s UI thread. All updates to visual elements must, therefore, be invoked on the UI thread, either directly by using the app’s global Dispatcher
or indirectly via a custom property change notification system, discussed later in this chapter.
Note
Avoid using DeviceState.PowerSource
for determining whether to use the phone’s network connection to transfer a substantial amount of data. See Chapter 27, “Communicating with Network Services,” to learn how to monitor network connectivity and how to determine the type of network connection being used.
18.189.193.210