【发布时间】:2020-11-27 04:54:18
【问题描述】:
我正在将我的项目从 C++/CX 移植到 C++/WinRT。为此,我需要做一些这样的互操作:https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/interop-winrt-cx。
Microsoft 建议使用此类帮助函数以实现互操作性。
from_cx 和 to_cx 函数 下面的帮助函数将 C++/CX 对象转换为等效的 C++/WinRT 对象。该函数将 C++/CX 对象转换为其底层 IUnknown 接口指针。然后,它在该指针上调用 QueryInterface 以查询 C++/WinRT 对象的默认接口。 QueryInterface 是与 C++/CX safe_cast 扩展等效的 Windows 运行时应用程序二进制接口 (ABI)。而且,winrt::put_abi 函数检索 C++/WinRT 对象的底层 IUnknown 接口指针的地址,以便可以将其设置为另一个值。
template <typename T>
T from_cx(Platform::Object^ from)
{
T to{ nullptr };
winrt::check_hresult(reinterpret_cast<::IUnknown*>(from)
->QueryInterface(winrt::guid_of<T>(),
reinterpret_cast<void**>(winrt::put_abi(to))));
return to;
}
下面的帮助函数将 C++/WinRT 对象转换为等效对象 C++/CX 对象。 winrt::get_abi 函数检索指向 C++/WinRT 对象的基础 IUnknown 接口。函数转换 在使用 C++/CX safe_cast 之前指向 C++/CX 对象的指针 查询请求的 C++/CX 类型的扩展。
template <typename T>
T^ to_cx(winrt::Windows::Foundation::IUnknown const& from)
{
return safe_cast<T^>(reinterpret_cast<Platform::Object^>(winrt::get_abi(from)));
}
但是,当我这样做时:
auto text = winrt::Windows::UI::Xaml::Controls::TextBlock();
Windows::UI::Xaml::FrameworkElement^ cx = to_cx<Windows::UI::Xaml::FrameworkElement^>(text);
我收到一个错误:
没有函数模板“to_cx”的实例与参数列表匹配
参数类型有:(winrt::Windows::UI::Xanl::Controls::TextBlock)
但我确实看到 TextBlock 继承自 IUnknown。我错过了什么?
【问题讨论】:
标签: windows xaml uwp-xaml c++-cx c++-winrt