WP7 Dev 101 【4】 如何获取用户和手机的信息

转载地址:http://www.gwewe.com/dev/topics/1101151810.html

在我们写程序的时候,经常需要知道谁在使用这个程序或者说是程序运行在什么样的手机上。相比之前使用.NET CF来开发Windows Mobile系统上的应用程序来说,Windows Phone 7中提供的API就方便多了。

要获取用户或者Windows Phone 7的信息,首先需要在程序的WMAppManifest.xml中声明如下段落:

  1. <Capability Name=ID_CAP_IDENTITY_DEVICE/>
  2. <Capability Name=ID_CAP_IDENTITY_USER/>
 然后我们可以通过Microsoft.Phone.Info命名空间下的两个类“UserExtendedProperties”和“DeviceExtendedProperties”来分别获取用户和设备的一些信息。这两个类里面都只有两个静态方法“GetValue”和”TryGetValue”。你只需要传入想要查找的属性值就可以。

  目前来说,UserExtendedProperties里面只能查找到”ANID”这个值,它是将当前系统中的用户身份进行了某些运算之后得到的一个32位的字符串。同时,在模拟器上,或者在真实设备上如果没有绑定Windows Live ID的话,也获取不到任何数据。

  而对于DeviceExtendedProperties,可以获取的信息就相对多一些,例如设备的厂商,型号等等。而且也包含当前程序占用/峰值内存。

            string anid = UserExtendedProperties.GetValue("ANID") as string;
            System.Diagnostics.Debug.WriteLine("AnID: " + anid);
            string result = string.Empty;
            result = result + "Manufacturer:"+ DeviceExtendedProperties.GetValue("DeviceManufacturer").ToString() + System.Environment.NewLine;
            result = result + "DeviceName:"+ DeviceExtendedProperties.GetValue("DeviceName").ToString() + System.Environment.NewLine;
            result = result + "DeviceUniqueId:"+ DeviceExtendedProperties.GetValue("DeviceUniqueId") + System.Environment.NewLine;
            result = result + "DeviceFirmwareVersion:"+ DeviceExtendedProperties.GetValue("DeviceFirmwareVersion").ToString() + System.Environment.NewLine;
            result = result + "DeviceHardwareVersion:"+ DeviceExtendedProperties.GetValue("DeviceHardwareVersion").ToString() + System.Environment.NewLine;
            result = result + "DeviceTotalMemory:"+ DeviceExtendedProperties.GetValue("DeviceTotalMemory").ToString() + System.Environment.NewLine;
            result = result + "ApplicationCurrentMemoryUsage:" + DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage").ToString() + System.Environment.NewLine;
            result = result + "ApplicationPeakMemoryUsage:"+ DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage").ToString();
            System.Diagnostics.Debug.WriteLine(result);

原文地址:https://www.cnblogs.com/xingchen/p/1976856.html