5

在为 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()方法。

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

4

1 回答 1

10

IsTypePresent 不用于检测硬件存在,而是用于检测 API 存在。在您的代码片段中,它正在检查 HardwareButtons 类是否存在以供应用程序调用,而不是设备是否具有硬件按钮(在这种情况下,它们可能会一起使用,但这不是 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!
于 2015-06-12T19:17:14.000 回答