尝试使用 SharpDX 和 DXD11 在 Windows App SDK C# 中获取对 SwapChainPanel 的本机 COM 对象的引用时出现异常

Exception when trying to obtain a reference to the native COM object of the SwapChainPanel in Windows App SDK C# using SharpDX and DXD11

我正在尝试使用 SharpDX 获取对在 Windows App SDK C# 中创建的 SwapChainPanel 的本机 COM 对象的引用。

我将我的项目从 UWP 迁移到更新的 Windows App SDK with WinUI 3 and .Net 6 并且除了创建 SwapChainPael 之外一切正常。

UWP: https://github.com/CanTalat-Yakan/UWP-GameEngineEditor/blob/main/Editor/Views/View_Port.xaml.cs https://github.com/CanTalat-Yakan/UWP-GameEngineEditor/blob/main/Editor/Assets/Engine/Utilities/Engine_Renderer.cs

新Windows 应用程序 SDK: https://github.com/CanTalat-Yakan/3D_Engine-WinUI3/blob/main/WinUI3DEngine/WinUI3DEngine/WinUI3DEngine/UserControls/ViewPort.xaml.cs https://github.com/CanTalat-Yakan/3D_Engine-WinUI3/blob/main/WinUI3DEngine/WinUI3DEngine/WinUI3DEngine/Assets/Engine/Utilities/CRenderer.cs

出现上述错误的部分是CRenderer:71(ctor)

        // Obtain a reference to the native COM object of the SwapChainPanel.
        using (var nativeObject = ComObject.As<DXGI.ISwapChainPanelNative2>(m_SwapChainPanel))
            nativeObject.SwapChain = m_SwapChain;

异常详情:

SharpDX.SharpDXException HResult=0x80004002 Message=HRESULT: [0x80004002], Module: [General], ApiCode: [E_NOINTERFACE/No such interface supported], Message: No such interface supported

Source=SharpDX StackTrace: at SharpDX.Result.CheckError() at SharpDX.ComObject.QueryInterface(Guid guid, IntPtr& outPtr) at SharpDX.ComObject.QueryInterfaceT at SharpDX.ComObject.As[T](Object comObject) at WinUI3DEngine.Assets.Engine.Utilities.CRenderer..ctor(SwapChainPanel _swapChainPanel) in D:\Misc\Sourcetree\Engine\WinUI3DEngine\WinUI3DEngine\WinUI3DEngine\Assets\Engine\Utilities\CRenderer.cs:line 71 at WinUI3DEngine.Assets.Engine.CEngine..ctor(SwapChainPanel _swapChainPanel, TextBlock _textBlock) in D:\Misc\Sourcetree\Engine\WinUI3DEngine\WinUI3DEngine\WinUI3DEngine\Assets\Engine\CEngine.cs:line 18 at WinUI3DEngine.UserControls.ViewPort.Initialize(Object sender, RoutedEventArgs e) in D:\Misc\Sourcetree\Engine\WinUI3DEngine\WinUI3DEngine\WinUI3DEngine\UserControls\ViewPort.xaml.cs:line 41 at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.b__1_0(Object sender, RoutedEventArgs e) at ABI.Microsoft.UI.Xaml.RoutedEventHandler.<>c__DisplayClass10_0.<Do_Abi_Invoke>b__0(RoutedEventHandler invoke) at WinRT.ComWrappersSupport.MarshalDelegateInvoke[T](IntPtr thisPtr, Action`1 invoke) at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

我尝试从过程开始调试,一切都与 UWP 版本相似。使用新的 Windows App SDK,Windows.UI.Xaml.Controls 的命名空间更改为 Microsoft.UI.Xaml.Controls 我不太了解 SharpDX 包装器,但这可能是问题所在吗? 也许 SharpDX 根本不支持 .Net 5 或 6 只有 4 及以下? 我是否需要以不同方式设置 Device -Contex 和最后 SwapchainPanel 的创建?

如果我得到解决方案,如果是我的错误或其他错误,我会在下面post他们

基本上

windows.ui.xaml.media.dxinterop::ISwapChainPanelNative 与 UWP Windows.UI.Xaml.Controls.SwapChainPanel 控件一起工作

不适用于

WinUI3 Microsoft.UI.Xaml.Controls.SwapChainPanel 控件,它实现了一个不同的界面,该界面具有相同的名称但具有新的 GUID (63aad0b8-7c24-40ff-85a8-640d944cc325) 和命名空间:microsoft.ui.xaml.media.dxinterop::ISwapChainPanelNative

注意 Windows 与 Microsoft 的命名空间差异 https://githubhot.com/repo/amerkoleci/Vortice.Windows/issues/91

这里有一个解决方法: https://github.com/amerkoleci/Vortice.Windows/discussions/164

但是由于 SharpDX 被破坏,我切换到更新的包装器 Vortice https://github.com/amerkoleci/Vortice.Windows https://github.com/amerkoleci/Vortice.Windows/tree/main/src

支持WinUI 3和新的WindowsApp SDK(新项目重聚)