Windows Phone 硬件检测
介绍
随着Lumia的日益壮大,手机的硬件配置也越来越多样化,要想让你的应用在不同配置的手机上能更好的展现,作为开发者,你就不能不去了解一下手机的硬件配置。 Lumia系列WP8手机配置对比 并且,你还需要知道如何获取这些配置信息。
Windows Phone 8 手机分辨率
private static bool IsWvga { get { return App.Current.Host.Content.ScaleFactor == 100; } } private static bool IsWxga { get { return App.Current.Host.Content.ScaleFactor == 160; } } private static bool Is720p { get { return App.Current.Host.Content.ScaleFactor == 150; } }
硬件配置
摄像头(Camera)
#if WINDOWS_PHONE_8 // Windows Phone 8 try { BackCameraExist = PhotoCaptureDevice.AvailableSensorLocations.Contains<CameraSensorLocation>(CameraSensorLocation.Back); FrontCameraExist = PhotoCaptureDevice.AvailableSensorLocations.Contains<CameraSensorLocation>(CameraSensorLocation.Front); } catch (System.Exception e) { MessageBox.Show(e.Message); } #else // Windows Phone 7 FrontCameraExist = Microsoft.Devices.Camera.IsCameraTypeSupported(Microsoft.Devices.CameraType.FrontFacing); BackCameraExist = Microsoft.Devices.Camera.IsCameraTypeSupported(Microsoft.Devices.CameraType.Primary); #endif
罗盘(Compass)
#if WINDOWS_PHONE_8 // Windows Phone 8 if (Compass.GetDefault() != null) { CompassExist = true; } #else // Windows Phone 7 CompassExist = Microsoft.Devices.Sensors.Compass.IsSupported; #endif
NFC
#if WINDOWS_PHONE_8 // Windows Phone 8 // ID_CAP_NETWORKING ID_CAP_PROXIMITY if (ProximityDevice.GetDefault() != null) { ProximityExist = true; } #else // Windows Phone 7 // Windows Phone 7 不支持NFC功能 #endif
陀螺仪(Gyroscope)
#if WINDOWS_PHONE_8 // Windows Phone 8 // 目前Gyrometer.GetDefault()在陀螺仪不支持时会抛出异常,以MSDN文档描述的返回null不符。 try { if (Gyrometer.GetDefault() != null) { GyroscopeExist = true; } } catch (Exception e) { MessageBox.Show(e.Message); } #else // Windows Phone 7 GyroscopeExist = Microsoft.Devices.Sensors.Gyroscope.IsSupported; #endif
加速传感器(Accelerometer)
#if WINDOWS_PHONE_8 // Windows Phone 8 if (Accelerometer.GetDefault() != null) { AccelerometerExist = true; } #else // Windows Phone 7 AccelerometerExist = Microsoft.Devices.Sensors.Accelerometer.IsSupported; #endif
磁倾仪传感器(Inclinometer)
#if WINDOWS_PHONE_8 // Windows Phone 8 if (Windows.Devices.Sensors.Inclinometer.GetDefault() != null) { InclinometerExist = true; } #else // Windows Phone 7 // Windows Phone 7 不支持 #endif
方向传感器(Orientation)
#if WINDOWS_PHONE_8 // Windows Phone 8 if (Windows.Devices.Sensors.OrientationSensor.GetDefault() != null) { OrientationSensorExist = true; } #else // Windows Phone 7 OrientationSensorExist = Microsoft.Devices.Sensors.Motion.IsSupported; #endif
震动设备(VibrationDevice)
#if WINDOWS_PHONE_8 // Windows Phone 8 if (Windows.Phone.Devices.Notification.VibrationDevice.GetDefault() != null) { VibrationDeviceExist = true; } #else // Windows Phone 7 if (Microsoft.Devices.VibrateController.Default != null) { VibrationDeviceExist = true; } #endif
电量传感器(Battery)
#if WINDOWS_PHONE_8 // Windows Phone 8 if (Windows.Phone.Devices.Power.Battery.GetDefault() != null) { BatterySensorExist = true; } #else // Windows Phone 7 // Windows Phone 7 不支持 #endif
内存判断(Memory)
#if WINDOWS_PHONE_8 // Windows Phone 8 MemoryCurrentUsed = Windows.Phone.System.Memory.MemoryManager.ProcessCommittedBytes.ToString(); MemoryMaxAvailable = Windows.Phone.System.Memory.MemoryManager.ProcessCommittedLimit.ToString(); #else // Windows Phone 7 // 通过DeviceExtendedProperties类获取 #endif
Windows Phone 应用的功能和硬件要求
在获取设备硬件信息之前,你还 需要在清单文件中标记你的应用需要的功能(CAPABILITIES) 。另外你还 可以在清单文件中指定应用的硬件要求(REQUIREMENTS),以确保只有满足硬件要求的设备才能在应用商店中查看并下载安装你的应用。
要了解应用功能和硬件要求,可以在下面链接查看: http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/jj206936.aspx
应用清单文件
文件路径:工程目录->Properties->WMAppManifest.xml
应用清单文件设计器(Windows Phone 8)
我们可以通过清单文件设计器来标记应用的功能和硬件要求,但是在设计器中不能够标记全部的功能或硬件要求(比如ID_REQ_MEMORY_300),这时你就需要用“查看代码”的方式打开WMAppManifest.xml文件进行编辑。
示例代码
http://www.developer.nokia.com/Community/Wiki/Windows_Phone_%E7%A1%AC%E4%BB%B6%E6%A3%80%E6%B5%8B
本文介绍了如何在Windows Phone应用中检测各种硬件配置,包括分辨率、摄像头、罗盘等,并提供了示例代码。此外还讨论了如何在应用清单文件中声明所需的功能和硬件要求。





1万+

被折叠的 条评论
为什么被折叠?



