Partager via


How to use the DeviceStatus class for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

You can use the DeviceStatus API to determine status info about the device, such as the total memory of the device, the hardware version of the device, and whether a physical keyboard has been deployed. In addition, you can register for several events that notify your app when certain properties have changed.

The guidance and code examples in this topic are based on an end-to-end code sample called Device Status Sample.

This topic contains the following sections.

Accessing DeviceStatus properties

The following code example shows you how to access the properties of the DeviceStatus class and output the values into the Text property of TextBlock controls.

deviceManufacturerTextBlock.Text = DeviceStatus.DeviceManufacturer;
deviceNameTextBlock.Text = DeviceStatus.DeviceName;
deviceFirmwareVersionTextBlock.Text = DeviceStatus.DeviceFirmwareVersion;
deviceHardwareVersionTextBlock.Text = DeviceStatus.DeviceHardwareVersion;
deviceTotalMemoryTextBlock.Text = DeviceStatus.DeviceTotalMemory.ToString();
appCurrentMemoryUsageTextBlock.Text = DeviceStatus.ApplicationCurrentMemoryUsage.ToString();
appMemoryUsageLimitTextBlock.Text = DeviceStatus.ApplicationMemoryUsageLimit.ToString();
appPeakMemoryUsageTextBlock.Text = DeviceStatus.ApplicationPeakMemoryUsage.ToString();
isKeyboardPresentTextBlock.Text = DeviceStatus.IsKeyboardPresent.ToString();
isKeyboardDeployedTextBlock.Text = DeviceStatus.IsKeyboardDeployed.ToString();
powerSourceTextBlock.Text = DeviceStatus.PowerSource.ToString();

Determining the keyboard status

First, set up the event handler, as shown in the following code example.

DeviceStatus.KeyboardDeployedChanged += new EventHandler(DeviceStatus_KeyboardDeployedChanged);

Next, invoke a Dispatcher to update the Text property of a TextBlock control, as shown in the following code example.

void DeviceStatus_KeyboardDeployedChanged(object sender, EventArgs e)
{
    // The KeyboardDeployedChanged event is not raised on the UI thread, 
    // so the Dispatcher must be invoked to update the Text property.
    this.Dispatcher.BeginInvoke(() => 
        {
            isKeyboardDeployedTextBlock.Text = DeviceStatus.IsKeyboardDeployed.ToString();
        }
    );
}

Determining the current power source

First, set up the event handler, as shown in the following code example.

DeviceStatus.PowerSourceChanged += new EventHandler(DeviceStatus_PowerSourceChanged);

Next, invoke a Dispatcher to update the Text property of a TextBlock control, as shown in the following code example.

void DeviceStatus_PowerSourceChanged(object sender, EventArgs e)
{
    // The PowerSourceChanged event is not raised on the UI thread, 
    // so the Dispatcher must be invoked to update the Text property.
    this.Dispatcher.BeginInvoke(() => 
        {
            powerSourceTextBlock.Text = DeviceStatus.PowerSource.ToString();
        }
    );
}

See Also

Other Resources

Device status for Windows Phone 8