当我试图从C++ UWP应用程序中引用C# UWP类库中的方法时,我得到了一个COMException。 这是在最基本的设置下发生的,所以我一定是做错了什么。
复制。
public static int GetNumber() { return 22; }
using namespace ClassLib;
MainPage::MainPage()
{
InitializeComponent();
auto foo = Class1::GetNumber();
}
Exception thrown at 0x76984402 in UWPApp.exe:
Microsoft C++ exception:
Platform::COMException ^ at memory location 0x0421DD44.
HRESULT:0x80131040 The text associated with this error code could not be found.
这个问题是由于从C++CX或C++ WinRT项目中调用基于.net的WinRT组件。为了使它工作,你可以添加 Microsoft.Net.Native.Compiler
在你的c++cx项目中先安装nuget包。然后 右键点击项目-> 卸载项目-> 编辑.vcxproj。. 之后,在其中添加以下属性。
<PropertyGroup>
<UseDotNetNativeToolchain Condition="'$(Configuration)'=='Release'">true</UseDotNetNativeToolchain>
<DotNetNativeVersion>2.2.3</DotNetNativeVersion>
</PropertyGroup>
注意,用你安装的Microsoft.Net.Native.Compiler nuget包的版本替换上面的2.2.3版本。更多关于它的细节,你可以参考这篇类似的文章。纫.