Device(裝置)之硬體狀態, 系統狀態, 網路狀態
介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之裝置
硬體狀態
系統狀態
網路狀態
樣本
1、示範如何擷取硬體的相關狀態
HardwareStatus.xaml.cs
/* * 示範如何擷取裝置的硬體資訊 */ using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using System.Windows.Navigation; using Microsoft.Phone.Info; namespace Demo.Device.Status { public partial class HardwareStatus : PhoneApplicationPage { public HardwareStatus() { InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { lblMsg.Text = ""; /* * DeviceStatus - 用於擷取相關的裝置資訊 */ lblMsg.Text += "裝置製造商:" + DeviceStatus.DeviceManufacturer; lblMsg.Text += Environment.NewLine; lblMsg.Text += "裝置名稱:" + DeviceStatus.DeviceName; lblMsg.Text += Environment.NewLine; lblMsg.Text += "實體記憶體總計:" + DeviceStatus.DeviceTotalMemory; // 單位:位元組 lblMsg.Text += Environment.NewLine; lblMsg.Text += "系統分給當前應用程式的最大可用記憶體:" + DeviceStatus.ApplicationMemoryUsageLimit; // 單位:位元組 lblMsg.Text += Environment.NewLine; lblMsg.Text += "當前應用程式佔用記憶體的當前值:" + DeviceStatus.ApplicationCurrentMemoryUsage; // 單位:位元組 lblMsg.Text += Environment.NewLine; lblMsg.Text += "當前應用程式佔用記憶體的高峰值:" + DeviceStatus.ApplicationPeakMemoryUsage; // 單位:位元組 lblMsg.Text += Environment.NewLine; lblMsg.Text += "硬體版本:" + DeviceStatus.DeviceHardwareVersion; lblMsg.Text += Environment.NewLine; lblMsg.Text += "韌體版本:" + DeviceStatus.DeviceFirmwareVersion; lblMsg.Text += Environment.NewLine; lblMsg.Text += "裝置是否包含物理鍵盤:" + DeviceStatus.IsKeyboardPresent; lblMsg.Text += Environment.NewLine; lblMsg.Text += "物理鍵盤是否正在使用:" + DeviceStatus.IsKeyboardDeployed; lblMsg.Text += Environment.NewLine; /* * Microsoft.Phone.Info.PowerSource 枚舉 - 供電方式 * Battery - 電池 * External - 外接電源 */ lblMsg.Text += "供電方式:" + DeviceStatus.PowerSource; lblMsg.Text += Environment.NewLine; lblMsg.Text += "是否支援多解析度編碼視頻的Smooth Streaming:" + MediaCapabilities.IsMultiResolutionVideoSupported; lblMsg.Text += Environment.NewLine; lblMsg.Text += "裝置標識:" + GetDeviceUniqueId(); // 當物理鍵盤的使用狀態(使用或關閉)發生改變時所觸發的事件 DeviceStatus.KeyboardDeployedChanged += new EventHandler(DeviceStatus_KeyboardDeployedChanged); // 當裝置的供電方式(電池或外接電源)發生改變時所觸發的事件 DeviceStatus.PowerSourceChanged += new EventHandler(DeviceStatus_PowerSourceChanged); } void DeviceStatus_PowerSourceChanged(object sender, EventArgs e) { MessageBox.Show("供電方式:" + DeviceStatus.PowerSource); } void DeviceStatus_KeyboardDeployedChanged(object sender, EventArgs e) { MessageBox.Show("物理鍵盤是否正在使用:" + DeviceStatus.IsKeyboardDeployed); } /// <summary> /// 擷取裝置的唯一ID /// </summary> private string GetDeviceUniqueId() { string result = ""; object uniqueId; /* * DeviceExtendedProperties.TryGetValue() - 用於擷取裝置的唯一ID */ if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId)) { result = ByteToHexStr((byte[])uniqueId); if (result != null && result.Length == 40) result = result.Insert(8, "-").Insert(17, "-").Insert(26, "-").Insert(35, "-"); } return result; } /// <summary> /// 將一個位元組數群組轉換為十六進位字串 /// </summary> private string ByteToHexStr(byte[] bytes) { string returnStr = ""; if (bytes != null) { for (int i = 0; i < bytes.Length; i++) { returnStr += bytes[i].ToString("X2"); } } return returnStr; } } }