如何在cppwinrt 中访问DeviceInformation class 的成员?

How to access members of DeviceInformation class in cppwinrt?

winrt MIDI 文档示例代码建议,给定一个 DeviceInformation 对象,可以通过引用 DeviceInformation 的 Id 创建 MIDIPort,就像这样,对于名为 devInfo 的 DeviceInformation:

midiOutPort = 等待 MidiOutPort.FromIdAsync(devInfo.Id);

当然,在 cppwinrt 中,人们会使用它的 C++ 版本,但症结在于访问 devInfo 的 ID(无论是通过 devInfo.Id() 还是 devInfo.Id 或其他)。报错是"DeviceInformation does not have a member named Id." cppwinrt中肯定有这个但是我没有找到访问的方法。

如果相关,我是这样声明设备信息的:

winrt::Windows::Foundation::Collections::IIterator<winrt::Windows::Devices::Enumeration::DeviceInformation> devInfo;

因为 winrt::Windows::Devices::Enumeration::DeviceInformation 在枚举 DeviceInformationCollection 时未被接受。

您正在处理 IIterator<DeviceInformation>, not DeviceInformation. To extract the data from IIterator, you need to call Current()。因此,在您的示例中:

auto id = devInfo.Current().Id();

此外,C++/WinRT 集合支持基于范围的 for 循环,因此您可以绕过 IIterator 并直接迭代集合,如下所示:

DeviceInformationCollection collection = ...; // Some initialization
for (const auto& info : collection)
{
  auto id = info.Id();
}