无法引用包含合并字典的资源字典 [英] Trouble referencing a Resource Dictionary that contains a Merged Dictionary

查看:61
本文介绍了无法引用包含合并字典的资源字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库 CommonLibraryWpfThemes,里面有几个资源字典 XAML 文件.我的 Themes/Generic.xml 文件包含将所有其他文件合并在一起的 ResourceDictionary.MergedDictionaries 声明.

I have a library, CommonLibraryWpfThemes, with several Resource Dictionary XAML files in it. My Themes/Generic.xml file contains a ResourceDictionary.MergedDictionaries declaration that merges all the other files together.

Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" />
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" />
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" />
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" />
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

在我的应用程序项目中,我引用了 CommonLibraryWpfThemes,并且我在 App.xaml 文件中明确引用了 Generic.xml.

In my application project, I have a reference to CommonLibraryWpfThemes, and I explicitly reference Generic.xml in my App.xaml file.

App.xaml -- 失败

<Application
    x:Class="MyApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary
            Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />
    </Application.Resources>
</Application>

这不起作用.运行我的应用程序时出现以下错误:

This doesn't work. I get the following error when I run my app:

System.Windows.Markup.XamlParseException occurred
  Message="Cannot find resource named '{_fadedOrangeBrush}'. Resource names are case sensitive.  Error at object 'System.Windows.Setter' in markup file 'CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml' Line 18 Position 13."
  Source="PresentationFramework"
  LineNumber=18
  LinePosition=13

如果我将 Generic.xaml 的内容直接放入 App.xaml 中,一切正常:

If I place the contents of Generic.xaml into App.xaml directly, everything works fine:

App.xaml -- 成功

<Application
    x:Class="MyApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary
                    Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" />
                <ResourceDictionary
                    Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" />
                <ResourceDictionary
                    Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" />
                <ResourceDictionary
                    Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" />
                <ResourceDictionary
                    Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

<小时>

也许我以错误的方式处理这个问题.我的目标是使从多个应用程序中引用我的所有主题资源变得容易,而不必列出所有单个文件.有没有推荐的方法来做到这一点?(注意:我不想在多个主题之间切换——我只有一个主题.)


Maybe I'm going about this in the wrong way. My goal is to make it easy to reference all my theme resources from multiple applications without having to list out all the individual files. Is there a recommended way to do this? (Note: I'm not trying to switch between multiple themes--I just have one theme.)

作为奖励,如果有人能告诉我如何在不破坏 Visual Studio 中的设计器的情况下引用外部库中的资源,那就太好了.

As a bonus, it would be nice if someone could tell me how to reference resources in an external library without breaking the designer in Visual Studio.

谢谢.

我尝试将 ResourceDictionary 包装在 ResourceDictionary.MergedDictionary 元素中,但这也不起作用(我遇到了同样的错误):

I tried wrapping the ResourceDictionary in a ResourceDictionary.MergedDictionary element, but that also didn't work (I get the same error):

<Application
    x:Class="MyApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary
                    Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

推荐答案

之前在这里回答了一个类似的问题,参见 将合并字典添加到合并字典 问题.

Answered a similar question here earlier, see Adding a Merged Dictionary to a Merged Dictionary question.

这是一个优化错误,请参阅 内部 MergedDictionaries 中未找到 Microsoft Connect/DefaultStyleKey 样式:

This is an optimization bug, see Microsoft Connect / DefaultStyleKey style not found in inner MergedDictionaries:

关于每个对象的创建XAML,如果存在默认样式(即带有类型键的样式)样式应该被应用.尽你所能想象一下有几种表现优化以实现(暗示)查找重量尽可能轻.一其中之一是我们不向内看资源字典,除非它们是标记为包含默认风格".有一个错误:如果你所有的默认样式嵌套在合并中三层深的字典(或更深)顶级词典没有被标记,因此搜索会跳过它.解决方法是设置一个默认值风格的东西,任何东西,在根字典.

On the creation of every object in XAML, if a default style is present (i.e. style w/ a key of Type) that style should be applied. As you can imagine there are several performance optimizations to make that (implied) lookup a light weight as possible. One of them is that we don’t look inside Resource Dictionaries unless they are flagged as "containing default Styles". There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it. The work around is to put a default Style to something, anything, in the root Dictionary.

所以在根字典中添加一个虚拟样式可以解决这个问题.示例

So adding a dummy style to the root dictionary fixes this. Example

<Application x:Class="MyApp.App"  
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  
    <Application.Resources> 
        <ResourceDictionary> 
            <ResourceDictionary.MergedDictionaries> 
                <ResourceDictionary 
                    Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" /> 
                </ResourceDictionary.MergedDictionaries> 
            <!-- Dummy Style, anything you won't use goes --> 
            <Style TargetType="{x:Type Rectangle}" /> 
        </ResourceDictionary> 
    </Application.Resources> 
</Application>   

这篇关于无法引用包含合并字典的资源字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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