×
Namespaces

Variants
Actions

Windows Phone 硬件检测

From Nokia Developer Wiki
Jump to: navigation, search
WP Metro Icon WP8.png
Article Metadata

代码示例
兼容于
文章
Xinx Gong 在 01 Apr 2013 创建
最后由 hamishwillee 在 18 Jul 2013 编辑

Contents

介绍

随着Lumia阵营的日益壮大,越来越多的WP8机型上市,手机的硬件配置也越来越多样化。作为开发者,要想让你的代码更健壮,使你的应用在不同配置的手机上都能更好的展现,你就不能不去了解一下手机的硬件配置, Lumia系列WP8手机配置对比 并且,你还需要知道如何获取这些配置信息,并在代码中加入判断它们是否存在的逻辑。

Windows Phone 8 手机分辨率

Hardware Finder 1.png

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

闪光灯(Flash)

    if (BackCameraExist)
{
var cam = new Microsoft.Devices.PhotoCamera(Microsoft.Devices.CameraType.Primary);
BackCameraFlashExist = cam.IsFlashModeSupported(Microsoft.Devices.FlashMode.On);
}
 
if (FrontCameraExist)
{
var cam = new Microsoft.Devices.PhotoCamera(Microsoft.Devices.CameraType.FrontFacing);
FrontCameraFlashExist = cam.IsFlashModeSupported(Microsoft.Devices.FlashMode.On);
}

罗盘(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


SD卡(SD Card)

#if WINDOWS_PHONE_8 // Windows Phone 8
var devices = await ExternalStorage.GetExternalStorageDevicesAsync();
SDCardExist = (devices != null && devices.Count() > 0);
#else // Windows Phone 7
// Windows Phone 7 不支持SD卡
#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

Hardware Finder 2.png

应用清单文件设计器(Windows Phone 8)

Hardware Finder 3.png

Hardware Finder 4.png

我们可以通过清单文件设计器来标记应用的功能和硬件要求,但是在设计器中不能够标记全部的功能或硬件要求(比如ID_REQ_MEMORY_300),这时你就需要用“查看代码”的方式打开WMAppManifest.xml文件进行编辑。

Hardware Finder 5.png

示例代码

File:HardwareFinder.zip

This page was last modified on 18 July 2013, at 05:02.
343 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×