我正在将我的项目从 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。我错过了什么?