嵌套资源字典在单独的库中 [英] Nested Resource Dictionary in separate library

查看:105
本文介绍了嵌套资源字典在单独的库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与这个一个

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

I have a solution with a number of projects. The are two that are relevant: a class library that contains a WPF window and a project with all the WPF styles in it.

项目1中带有窗口的类库

窗口的合并字典是

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

            //other styles here

中的CommonStyle.xaml项目2

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

这会导致以下错误:


{无法找到资源'components / type / checkbox.xaml'。}

{"Cannot locate resource 'components/type/checkbox.xaml'."}

但是,如果我在Project 1中创建了CommonStyle.xaml,并从Project 2中引用了相同的控件样式,然后它就起作用了。

However, if I create a CommonStyle.xaml in Project 1 and reference the same control styles from Project 2 then it works.

如何制作最高级别的xaml文件(CommonStyle.xaml )在Project 2中工作?

How do I make that highest level xaml file (CommonStyle.xaml) work from the Project 2?

推荐答案

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

I am currently unable to test this, however I hope it should work.

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

Rather than a rooted path, use a relative path in Project 2, i.e.

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

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

You may also use .. as required to navigate up to a relative directory (depending upon the location of CommonStyle.xaml), e.g.

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

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

I believe when you use a rooted path (starting with /) it will look for CheckBox.xaml in the root of the project where you use CommonStyle.xaml rather than relative to the location of CommonStyle.xaml.

其他说明

从您的描述看来,您似乎具有以下结构:

From you have described, it seems you have the following structure:

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

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

When CommonStyle.xaml refers to / it is normally referring to the root of Project 2, however when you merge this into Window.xaml the / would now refer to the root of Project 1, subsequently it is unable to locate Components/Type/CheckBox.xaml.

通过删除 / ,它将现在可以相对于 CommonStyle.xaml 的位置查找 Components / Type / CheckBox.xaml

By removing the / it will now look for Components/Type/CheckBox.xaml relative to the location of CommonStyle.xaml which it is able to do.

这篇关于嵌套资源字典在单独的库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆