如何从C++ UWP应用中引用C# UWP类库?

问题描述 投票:0回答:1

当我试图从C++ UWP应用程序中引用C# UWP类库中的方法时,我得到了一个COMException。 这是在最基本的设置下发生的,所以我一定是做错了什么。

复制。

  1. 使用Visual Studio(我使用16.5.4),创建一个新的 "空白应用程序(Universal Windows - c++CX)"
  2. 在解决方案中添加一个新的 "Windows运行时组件(通用Windows)",C#,称为 "ClassLib" 。
  3. 将此方法添加到Class1.cs中。public static int GetNumber() { return 22; }
  4. 将MainPage构造函数修改成这样。
using namespace ClassLib;
MainPage::MainPage()
{
    InitializeComponent();

    auto foo = Class1::GetNumber();
}
  1. 执行应用程序。 这个异常发生在MainPage构造函数中。
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# c++ uwp win-universal-app c++-cx
1个回答
2
投票

这个问题是由于从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版本。更多关于它的细节,你可以参考这篇类似的文章。.

© www.soinside.com 2019 - 2024. All rights reserved.