如何从 SwapChainPanel IntPtr 使用 ISwapChainPanelNative 接口

How to consume ISwapChainPanelNative interface from SwapChainPanel IntPtr

我从 C# 代码创建 Microsoft.UI.Xaml.Controls.SwapChainPanel 实例并像这样获取它的 IntPtr:

    WinRT.IObjectReference nativeObject = ((IWinRTObject)MySwapChainPanel).NativeObject;
    IntPtr panelIntPtr = nativeObject.ThisPtr;

    var cli = new MyCliClass();
    cli.SetRenderer(panelIntPtr);

在 C++/CLI 项目中,我尝试这样使用它:

#include <d3d11_2.h>
#include <d2d1_2.h>
#include <windows.ui.xaml.media.dxinterop.h>

void TestPlayerCli::MyCliClass::SetRenderer(System::IntPtr swapChainPtr)
{
    ISwapChainPanelNative* panelNative;
    reinterpret_cast<IUnknown*>(swapChainPtr.ToPointer())->QueryInterface(IID_PPV_ARGS(&panelNative));
    //not working either
    //panelNative = (ISwapChainPanelNative*)swapChainPtr.ToPointer();

    IDXGISwapChain2* m_swapChain;
    //init m_swapChain code...
    HRESULT hr = panelNative->SetSwapChain(m_swapChain);
}

在这种情况下,从提供的 swapChainPtr 获取 ISwapChainPanelNative 对象的正确方法是什么?

对于 WinUI3,您需要 #include <microsoft.ui.xaml.media.dxinterop.h> 而不是 <windows。 ui.xaml.media.dxinterop.h>.