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
应用清单文件设计器(Windows Phone 8)
我们可以通过清单文件设计器来标记应用的功能和硬件要求,但是在设计器中不能够标记全部的功能或硬件要求(比如ID_REQ_MEMORY_300),这时你就需要用“查看代码”的方式打开WMAppManifest.xml文件进行编辑。
示例代码
This page was last modified on 18 July