3

我的问题与这个非常相似

我有许多项目的解决方案。其中有两个是相关的:一个包含 WPF 窗口的类库和一个包含所有 WPF 样式的项目。

项目 1 中带有窗口的类库

Window 的合并字典类似于:

  <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary Source="pack://application:,,,/StyleAssembly;component/CommonStyle.xaml"/>
  </ResourceDictionary.MergedDictionaries> 

            //other styles here

项目 2中的 CommonStyle.xaml :

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Components/Type/CheckBox.xaml"/>
</ResourceDictionary.MergedDictionaries>

这会导致如下错误:

{“找不到资源‘components/type/checkbox.xaml’。”}

但是,如果我在项目 1 中创建一个 CommonStyle.xaml 并从项目 2 中引用相同的控件样式,那么它就可以工作。

如何使最高级别的 xaml 文件 (CommonStyle.xaml) 从项目 2 中工作?

4

1 回答 1

3

我目前无法对此进行测试,但是我希望它应该可以工作。

而不是根路径,在项目 2 中使用相对路径,即

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Components/Type/CheckBox.xaml"/>
</ResourceDictionary.MergedDictionaries>

您还可以..根据需要使用向上导航到相对目录(取决于 的位置CommonStyle.xaml),例如

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="../Components/Type/CheckBox.xaml"/>
</ResourceDictionary.MergedDictionaries>

我相信当您使用有根路径(以 开头/)时,它会CheckBox.xaml在您使用的项目的根目录中查找,CommonStyle.xaml而不是相对于CommonStyle.xaml.

附加说明

根据您的描述,您似乎具有以下结构:

- Project 1
  - Window.xaml
- Project 2
  - CommonStyle.xaml
  - Components
    - Type
      - CheckBox.xaml

CommonStyle.xaml引用/它通常是指项目2的根,但是当你将它合并到Window.xaml现在/将引用项目1的根时,它无法找到Components/Type/CheckBox.xaml

通过删除/它,它现在将寻找Components/Type/CheckBox.xaml相对于CommonStyle.xaml它能够执行的位置。

于 2014-07-30T14:12:54.767 回答