UWP 中缺少 xaml.xr 个 class 库文件

Missing xaml.xr of a class library file in UWP

我有一个 class 库项目,我正在 UWP 项目中使用它。如果我在 class 库中添加任何用户控件 xaml 文件并构建它,它就可以正常构建。但是UWP项目报错找不到.xr文件

我是否需要将任何文件夹中的外部 .xr 文件添加到我的 UWP 项目?

是的,对于一个包含XAML个文件的class库,如果我们想在其他项目中引用dll,我们不仅需要dll本身,还需要.xr.xaml文件和一些其他文件。因为在UWP环境下,资源不再嵌入到程序集中,而是作为内容放在dll旁边。参见类似案例:.

The files we need to reference like following:

  • ClassLibrary1(Class Library name) Folder
    • ClassLibrary1.xr.xml
    • UserControl.xaml (UserControl XAML file)
  • ClassLibrary1.dll
  • ClassLibrary1.pri

To get these files, we can check the "Generate library layout" option in the Build configuration under the project's Properties page.

然后我们可以将这些文件复制到任何地方,UWP项目只需要在Visual Studio中添加对ClassLibrary1.dll文件的引用,Visual Studio会自动拾取这些文件并在构建应用程序时将它们放入 appx 包中。