【问题标题】:Cannot use an Xaml component from a C# WinRT Component in C++/WinRT Xaml Islands project无法在 C++/WinRT Xaml Islands 项目中使用 C# WinRT 组件中的 Xaml 组件
【发布时间】:2020-12-08 10:45:32
【问题描述】:

如何复制(我们将以 Windows 终端为例):

  1. 克隆 Windows 终端存储库并获取子模块

  2. 使用带有一些随机控件的UserControl 在解决方案中创建一个 C# Windows 运行时组件

  3. 从您要在其中托管 UserControl 的项目中引用 C# WinRT 组件

  4. 在 Windows 终端项目之一中打开 Xaml 页面(我们将使用来自 TerminalAppLib 项目的 TerminalPage.xaml 作为示例)

  5. 将UserControl 添加到该页面

  6. 编译并运行(您可能需要将生成的 UserControl 到 pch.h 文件的 Xaml 编译文件包含在内,以便在没有 Xaml 编译器错误的情况下进行编译)

  7. 你会发现应用会因为class not registered异常而崩溃

我尝试过的:

  1. 使用Class Library 代替WinRT Component

  2. https://a.rcad.io/csharp-in-cppwinrt

  3. https://github.com/asklar/WinRTComponent/blob/master/README.md

  4. 在WindowsTerminal.manifest 文件中注册课程,像这样(当我尝试使用Class Library 时,我使用.dll 而不是.winmd):

<file name="myWinRTComponent.winmd" hashalg="SHA1" xmlns:winrt="urn:schemas-microsoft-com:winrt.v1">
    <winrt:activatableClass name="Namespace.UserControlClass" threadingModel="both" />
</file>
  1. 在AppxManifest.xml 文件中手动注册课程

【问题讨论】:

  • 您可以尝试将 xmlns 语句 xmlns:runtimecom="using:RuntimeComponent" 添加到 MainPage.xaml 文件中。
  • @YanGu-MSFT 这实际上是我用来将 C# WinRT 组件中的 UserControl 添加到 C++/WinRT Xaml Islands 项目中的页面中
  • 您可以尝试将 C# 组件的最低版本设置为 Windows 10 版本 1703 (Build 15063) 或更低版本:在您的 C# 项目中,转到 Properties、Library,然后在 Targeting 下将 Min version 设置为Windows 10 创意者更新(10.0;内部版本 15063)或更低版本。
  • @YanGu-MSFT 没用,同样的错误

标签: c# c++ xaml c++-winrt xaml-islands


【解决方案1】:

所以我终于找到了解决方案 我们必须注册 CLRHost.dll 而不是运行时组件 Winmd 文件

因此,如果应用程序是打包,我们必须将其添加到Extensions 标记内(它在Package 标记内,而不是在@ 内的那个Package.appxmanifest 的 987654324@ 标记)(请记住,Namespace 是控件的命名空间和运行时组件名称/程序集名称和 UserControlClass 是 UserControl 类的名称)

<Extension Category="windows.activatableClass.inProcessServer">
      <InProcessServer>
        <Path>CLRHost.dll</Path>
        <ActivatableClass ActivatableClassId="Namespace.Namespace_XamlTypeInfo.XamlMetaDataProvider" ThreadingModel="both" />
        <ActivatableClass ActivatableClassId="Namespace.UserControlClass" ThreadingModel="both" />
      </InProcessServer>
</Extension>

如果应用未打包,我们必须将其添加到Application.manifest 中的assembly 标记中(请记住Application 是应用程序和Namespace 是控件的命名空间和运行时组件名称/程序集名称和UserControlClass 是UserControl 类的名称)

<file name="CLRHost.dll" xmlns:winrt="urn:schemas-microsoft-com:winrt.v1">
    <winrt:activatableClass name="Namespace.Namespace_XamlTypeInfo.XamlMetaDataProvider" threadingModel="both"/>
    <winrt:activatableClass name="Namespace.UserControlClass" threadingModel="both"/>
</file>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    相关资源
    最近更新 更多