如何检测是在Windows 10 Universal Application中使用isTypePresent可以使用相机

Gor*_*ium 5 c# windows windows-10 windows-10-mobile

在开发适用于Windows 10的Universal Application时,建议您使用以下方法检测设备特定的硬件IsTypePresent.(Microsoft将此功能称为" 点亮 ").检查设备后退按钮的文档中的示例如下:

if(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;

这里很清楚,字符串"Windows.Phone.UI.Input.HardwareButtons"作为参数传递给IsTypePresent()方法.

我想知道是否有一种简单的方法来识别我可以用于其他硬件的其他字符串,特别是相机.

Rob*_*SFT 9

IsTypePresent不用于检测硬件存在,而是用于检测API存在.在你的代码片段中,它检查应用程序是否存在要调用的HardwareButtons类,而不是设备是否有硬件按钮(在这种情况下,它们可能会一起使用,但这不是IsTypePresent正在寻找的).

与相机一起使用的MediaCapture类是Universal API Contract的一部分,因此始终存在且可调用.如果没有合适的音频或视频设备,初始化将失败.

要查找硬件设备,可以使用Windows.Devices.Enumeration命名空间.这是一个快速片段,用于查询摄像头并查找第一个的ID.

var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);

if (devices.Count < 1)
{
    // There is no camera. Real code should do something smart here.
    return;
}

// Default to the first device we found
// We could look at properties like EnclosureLocation or Name
// if we wanted a specific camera
string deviceID = devices[0].Id;

// Go do something with that device, like start capturing!
Run Code Online (Sandbox Code Playgroud)