本文介绍了什么是的StaticResource内的一个WPF ResourceDictionary中的范围有多大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF ResourceDictionary中具有以下TextBlock的:

I have a WPF ResourceDictionary with the following TextBlock:

<TextBlock Visibility="{Binding Converter={StaticResource MyBoolProp ResourceKey=BoolToVis}}">
</TextBlock>

该ResourceDictionary的下MergedDictionaries包括在App.xaml中:

The ResourceDictionary is included in App.xaml under MergedDictionaries:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyResourceDictionary.xaml"/>

在App.xaml中我已经定义了BoolToVis转换器(再次,在 Application.Resources

Within the App.xaml I have defined the BoolToVis converter (again, under Application.Resources)

<BooleanToVisibilityConverter x:Key="BoolToVis" />

当我开始我的应用程序了 - 我得到以下XamlParseException:

When I start my app up - I get the following XamlParseException:

上提供的价值  System.Windows.Markup.StaticResourceHolder  引发了异常。

设置InnerException是:

The InnerException is:

无法找到资源名为'BoolToVis。资源名称是区分大小写的。

我能够参考这个转换器直接与App.xaml中(事实上,特定的XAML声明是相同的)和其它用户控件内,没有任何问题。

I'm able to refer to this converter directly with App.xaml (in fact, the particular XAML declaration is identical) and within other UserControls with no problems.

的code这个特殊的位也能正常工作在.NET 4.0 RC(和Beta2中)。该错误仅开始时,我升级到.NET 4.0 RTM发生。

This particular bit of code also worked fine under the .NET 4.0 RC (and Beta2). This error only started happening when I upgraded to the .NET 4.0 RTM.

我能解决它通过声明另一个 BooleanToVisibilityConverter 内MyResourceDictionary.xaml,并指它像这样:

I'm able to work around it by declaring another BooleanToVisibilityConverter within MyResourceDictionary.xaml and referring to it like so:

<TextBlock Visibility="{Binding Converter={StaticResource MyBoolProp ResourceKey=BoolToVis2}}">
</TextBlock>

有什么理由我需要这样做?

Any reason why I should need to do this?

推荐答案

MSDN

资源占用的资源查找范围,这仅仅是个主要的资源字典被合并到的范围后的位置。

在App.xaml中定义的资源不能被合并的ResourceDictionary看到。我倒觉得它更有意义的定义用于在ResourceDictionary中的ResourceDictionary中本身就是一个转换器,或其他的ResourceDictionary里面有所有的转换器。

Resources defined in App.xaml cannot be seen by a merged ResourceDictionary. I would think it makes more sense to define a converter used in a ResourceDictionary in the ResourceDictionary itself, or another ResourceDictionary which houses all your converters.

这篇关于什么是的StaticResource内的一个WPF ResourceDictionary中的范围有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:07