如何检测相机是否可在Windows 10通用应用程序中使用isTypePresent

问题描述:

在开发适用于Windows 10的通用应用程序时,建议您使用IsTypePresent检测设备特定的硬件。 (微软将此功能称为'Light up')。用于检查设备的后退按钮的example from the documentation是:如何检测相机是否可在Windows 10通用应用程序中使用isTypePresent

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()方法传递。

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

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

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

要找到硬件设备,您可以使用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!