从 UWP 应用程序 C# 项目调用 wrt/c++ Windows 运行时组件时出现 InvalidCastException
InvalidCastException when calling wrt/c++ Windows Runtime Component from UWP Application C# project
我有一个包含两个项目的小解决方案:
一个 UWP 应用程序 (C#)
a Windows 应用程序引用的运行时组件 (C++/WinRT)。
C++/WinRT 组件包含派生自 Windows.UI.Xaml.Controls.SwapChainPanel
的 class D3DPanel
。
代码编译,控件出现,并且在添加到 UWP C# 应用程序中的页面时工作正常。
但是,当我调用派生控件公开的单个 void StartRenderLoop()
方法时,我得到:
System.InvalidCastException 'Unable to cast object of type 'WRT_CPP.D3DPanel' to type 'WRT_CPP.ID3DPanel'.'
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)
at WRT_CPP.D3DPanel.StartRenderLoop()
at UWP_APP.MainPage.OnNavigatedTo(NavigationEventArgs e)
这是idl代码:
namespace WRT_CPP
{
[default_interface]
runtimeclass D3DPanel : Windows.UI.Xaml.Controls.SwapChainPanel
{
D3DPanel();
void StartRenderLoop();
}
}
这是实现 class 的声明方式:
struct D3DPanel : Windows::UI::Xaml::Controls::SwapChainPanelT<D3DPanel>
{
...
void StartRenderLoop();
...
}
(我知道该控件有效的原因是,如果我在 OnLoaded
期间从 WinRT 组件内部调用 StartRenderLoop()
,一切看起来都很好)。
可以在以下位置找到(几乎)最小版本的源代码:https://github.com/zrajnai/UWP_DX
要重现该问题,请注释掉 C++/Winrt 代码中对 StartRenderLoop 的调用,网址为:
https://github.com/zrajnai/UWP_DX/blob/227226e8dfbaf2b9b6ce78b6eb02c727c197e284/WRT_CPP/D3DPanel.cpp#L332
并取消注释抛出异常的行:
https://github.com/zrajnai/UWP_DX/blob/227226e8dfbaf2b9b6ce78b6eb02c727c197e284/UWP_APP/MainPage.xaml.cs#L19
感谢任何帮助。
问题出在实现上class
struct D3DPanel : Windows::UI::Xaml::Controls::SwapChainPanelT<D3DPanel>
我应该添加 ID3DPanel 作为通用参数以及实现 class 本身。
struct D3DPanel : Windows::UI::Xaml::Controls::SwapChainPanelT<D3DPanel, ID3DPanel>
我有一个包含两个项目的小解决方案:
一个 UWP 应用程序 (C#)
a Windows 应用程序引用的运行时组件 (C++/WinRT)。
C++/WinRT 组件包含派生自 Windows.UI.Xaml.Controls.SwapChainPanel
的 class D3DPanel
。
代码编译,控件出现,并且在添加到 UWP C# 应用程序中的页面时工作正常。
但是,当我调用派生控件公开的单个 void StartRenderLoop()
方法时,我得到:
System.InvalidCastException 'Unable to cast object of type 'WRT_CPP.D3DPanel' to type 'WRT_CPP.ID3DPanel'.'
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)
at WRT_CPP.D3DPanel.StartRenderLoop()
at UWP_APP.MainPage.OnNavigatedTo(NavigationEventArgs e)
这是idl代码:
namespace WRT_CPP
{
[default_interface]
runtimeclass D3DPanel : Windows.UI.Xaml.Controls.SwapChainPanel
{
D3DPanel();
void StartRenderLoop();
}
}
这是实现 class 的声明方式:
struct D3DPanel : Windows::UI::Xaml::Controls::SwapChainPanelT<D3DPanel>
{
...
void StartRenderLoop();
...
}
(我知道该控件有效的原因是,如果我在 OnLoaded
期间从 WinRT 组件内部调用 StartRenderLoop()
,一切看起来都很好)。
可以在以下位置找到(几乎)最小版本的源代码:https://github.com/zrajnai/UWP_DX
要重现该问题,请注释掉 C++/Winrt 代码中对 StartRenderLoop 的调用,网址为: https://github.com/zrajnai/UWP_DX/blob/227226e8dfbaf2b9b6ce78b6eb02c727c197e284/WRT_CPP/D3DPanel.cpp#L332
并取消注释抛出异常的行: https://github.com/zrajnai/UWP_DX/blob/227226e8dfbaf2b9b6ce78b6eb02c727c197e284/UWP_APP/MainPage.xaml.cs#L19
感谢任何帮助。
问题出在实现上class
struct D3DPanel : Windows::UI::Xaml::Controls::SwapChainPanelT<D3DPanel>
我应该添加 ID3DPanel 作为通用参数以及实现 class 本身。
struct D3DPanel : Windows::UI::Xaml::Controls::SwapChainPanelT<D3DPanel, ID3DPanel>