信息
您所需的主题如下所示。但此主题未包含在此库中。

如何使用 Windows Phone 的 DeviceStatus 类

2012/2/9

您可以使用 DeviceStatus API 确定有关设备的状态信息,如设备的总内存、设备的硬件版本以及是否部署了物理键盘。此外,您还可以注册多个事件,以便在某些属性发生更改时通知您的应用程序。

本主题中的指南和代码示例基于名为设备状态示例的端到端代码示例,该示例可以从 Windows Phone 的代码示例 下载。

以下代码示例显示如何访问 DeviceStatus 类的属性以及如何将值输出到 TextBlock 控件的 Text 属性中。

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

首先,设置事件处理程序,如以下代码示例所示。

DeviceStatus.KeyboardDeployedChanged += new EventHandler(DeviceStatus_KeyboardDeployedChanged);

接下来,调用 Dispatcher 来更新 TextBlock 控件的 Text 属性,如以下代码示例所示。

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

首先,设置事件处理程序,如以下代码示例所示。

DeviceStatus.PowerSourceChanged += new EventHandler(DeviceStatus_PowerSourceChanged);


接下来,调用 Dispatcher 来更新 TextBlock 控件的 Text 属性,如以下代码示例所示。

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

显示:
© 2014 Microsoft