windows-runtime - C++/WinRT 中 Platform::IBoxArray 的等效项

标签 windows-runtime c++-winrt

我目前正在将 UWP 应用程序从 C++/CX 移植到 C++/WinRT。我遇到了safe_cast<Platform::IBoxArray<byte>^>(data)哪里data类型为Windows::Foundation::IInspectable ^ .

我知道 safe_castas<T> 表示方法,我知道 WinRT/C++ 中有装箱 ( winrt::box_value ) 和拆箱 ( winrt::unbox_value ) 的函数。

但是,我需要知道 Platform::IBoxArray 的等价物为了执行转换(QueryInterface)。根据https://learn.microsoft.com/de-de/cpp/cppcx/platform-iboxarray-interface?view=vs-2017 , IBoxArray是 C++/CX 等价于 Windows::Foundation::IReferenceArray ,但没有winrt::Windows::Foundation::IReferenceArray ...

nackground 更新:我想要实现的是从其相机检索 HoloLens 附加到每个媒体基础样本的 View 变换。我的代码基于 https://github.com/Microsoft/HoloLensForCV ,除了最后一步之外,我确实一切正常。问题就出在这一段代码上:

static const GUID MF_EXTENSION_VIEW_TRANSFORM = {
    0x4e251fa4, 0x830f, 0x4770, 0x85, 0x9a, 0x4b, 0x8d, 0x99, 0xaa, 0x80, 0x9b
};

// ...

// In the event handler, which receives const winrt::Windows::Media::Capture::Frames::MediaFrameReader& sender:

auto frame = sender.TryAcquireLatestFrame();
// ...

if (frame.Properties().HasKey(MF_EXTENSION_VIEW_TRANSFORM)) {
    auto /* IInspectable */ userData = frame.Properties().Lookup(MF_EXTENSION_VIEW_TRANSFORM);

    // Now I would have to do the following:
    // auto userBytes = safe_cast<Platform::IBoxArray<Byte> ^>(userData)->Value;
    //viewTransform = *reinterpret_cast<float4x4 *>(userBytes.Data);
}

最佳答案

我还致力于将一些代码从 HoloLensForCV 移植到 C++/WinRT。我针对非常相似的情况提出了以下解决方案(但与您询问的代码行不完全相同):

auto user_data = source.Info().Properties().Lookup(c_MF_MT_USER_DATA); // type documented as 'array of bytes'
auto source_name = user_data.as<Windows::Foundation::IReferenceArray<std::uint8_t>>(); // Trial and error to get the right specialization of IReferenceArray
winrt::com_array<std::uint8_t> arr;
source_name.GetUInt8Array(arr);
winrt::hstring source_name_str{ reinterpret_cast<wchar_t*>(arr.data()) };

具体来说,您可以替换 safe_cast.as<Windows::Foundation::IReferenceArray<std::uint8_t>对于装箱的字节数组。然后,我怀疑做与我相同的转换(除了 float4x4* 而不是 wchar_t*)会对您有用。

上面的示例不需要/ZW 标志。

关于windows-runtime - C++/WinRT 中 Platform::IBoxArray 的等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54948092/

相关文章:

html - $.Ajax 在 Windows 8 Metro 应用程序中不起作用

C# Metro 应用程序如何从网络加载图像

c++ - 在cppwinrt中以编程方式创建图像按钮

c++ - 使用 WindowsCreateStringReference 与 WindowsCreateString 取消分配 HSTRING

c++ - 获取系统语言UWP C++/WinRT

windows-8 - Windows 8 的 Grid.IsSharedSizeScope 等效项

file-io - 无法从 WinRT 中的 xunit 访问文件(该进程没有包标识)

c# - 设置在本地文件夹 winRT 中保存字符串的文件的最大文件大小

c++ - 从 WinRT C++ 组件访问 COM 对象

c++-winrt - 如何读取更新的 C++/WinRT/XAML UWP 应用的命令行参数