首页 > 解决方案 > 一个程序集中的多个 XAML 资源文件,在另一个程序集中引用

问题描述

晚上好。请提出 WPF XAML 资源问题。

我有一个名为 Resources 的用户控制项目。在这个项目中,我有一个包含多个 XAML 文件的目录。我已将这些资源合并到项目根目录的 Main.xaml 文件中。

   <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Resources;component/xaml/Buttons.xaml" />
      <ResourceDictionary Source="Resources;component/xaml/Images.xaml" />
      <ResourceDictionary Source="Resources;component/xaml/Styles.xaml" />
      <ResourceDictionary Source="Resources;component/xaml/Tooltips.xaml" />      
   </ResourceDictionary.MergedDictionaries>

Resource 项目编译没有问题,并且您在上面看到的代码中没有视觉错误。

我有一个名为 Buttons 的第二个项目。在 App.xaml 中,我引用了这个 Resource 项目。

<Application.Resources>
  <ResourceDictionary>
     <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Resources;component/Main.xaml" />
     </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

我在 Button 项目中引用了 Resources.dll。Visual Studio 在 ResourceDictionary 语句下显示错误行;项目如何编译。

当我运行 Button.exe 时,我收到错误“无法找到资源 'resources;component/xaml/buttons.xaml'”。

我的 ResourceDictionary 语句与我在其他项目中成功完成的相匹配。假设我在 Button 项目中的 App.xaml 中编写的内容是正确的,我应该在 Resources 的 Main.xaml 文件中做些什么不同的事情?

提前致谢。

标签: wpfxamlresourcedictionary

解决方案


好吧,我想通了。我将记录如何执行此操作,以防其他人遇到此问题。

在我的一个名为 Resources 的程序集中,我在项目根目录中有一个 Main.xaml 文件。我有一个名为“xaml”的子目录,其中有几个 xaml 文件。我的错误是在目录“xaml”的前导反斜杠中。

   <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="xaml/Buttons.xaml" />
      <ResourceDictionary Source="xaml/Images.xaml" />
      <ResourceDictionary Source="xaml/Styles.xaml" />
      <ResourceDictionary Source="xaml/Tooltips.xaml" />      
   </ResourceDictionary.MergedDictionaries>

然后,在另一个名为 Buttons 的项目中,在 App.xaml 文件中,我在 MergedDictionaries 部分引用该 Main.xaml 文件。

<Application.Resources>
  <ResourceDictionary>
     <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Resources;component/Main.xaml" />
     </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

一旦我更正了主文件中的引用,一切正常。

因此,这允许我将我的资源拆分为单独的 xaml 文件并在其他程序集中引用它们。哇!


推荐阅读