wpf 为什么在这种情况下无法解析 StaticResource?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14454672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why the StaticResource cannot be resolved in this case?
提问by stukselbax
I have got an exception "Cannot find resource named 'mrg'. Resource names are case sensitive."when I try to do the following:
我有一个异常“找不到名为‘mrg’的资源。资源名称区分大小写。” 当我尝试执行以下操作时:
MainWindow.xaml:
主窗口.xaml:
<Window.Resources>
<Thickness Left="0"
Right="1"
Bottom="2"
Top="3"
x:Key="mrg" />
</Window.Resources>
<Grid>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:UserControl1 />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
MainWindow.xaml.cs:
主窗口.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<string> source = new List<string>()
{
"item1",
"item2",
"item3",
"item4",
"item5",
};
DataContext = source;
}
}
and UserControl1.xaml:
和 UserControl1.xaml:
<Grid>
<TextBlock Text="{Binding}" Margin="{StaticResource mrg}" />
</Grid>
According to the msdn article:
根据msdn文章:
Static resource lookup behavior
静态资源查找行为
The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.
The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. This continues until the root element is reached.
Next, application resources are checked. Application resources are those resources within the resource dictionary that is defined by the Application object for your WPF application.
查找过程在由设置属性的元素定义的资源字典中检查请求的键。
然后查找过程向上遍历逻辑树,到达父元素及其资源字典。这一直持续到到达根元素。
接下来,检查应用程序资源。应用程序资源是由 WPF 应用程序的 Application 对象定义的资源字典中的那些资源。
So the resource had to be found because of step 2. But, as I can see in the Localswindow when exception is catched, the UserControl1.Parent == null.
因此,由于第 2 步,必须找到资源。但是,正如我在Locals捕获异常时在窗口中看到的那样,UserControl1.Parent == null.
I'm confused in this problem. The way I can solve it is to put the resource to the Application level.
我对这个问题感到困惑。我可以解决的方法是将资源放到应用程序级别。
My question is: why the StaticResource connot be found ?
我的问题是:为什么找不到 StaticResource ?
回答by Clemens
The DataTemplate forms a logical tree of its own, which is disconnected from the logical tree of the ItemsControl. Hence the lookup by traversing the logical tree won't find the resource.
DataTemplate 形成了自己的逻辑树,它与 ItemsControl 的逻辑树是断开的。因此,遍历逻辑树的查找将找不到资源。
I wasn't able to find a reference in MSDN, just this article on CodeProject, where it reads:
我无法在 MSDN 中找到参考资料,只有这篇关于 CodeProject 的文章,它写道:
The elements that are part of an expanded template, hereafter referred to as "template elements", form their own logical tree which is disconnected from the logical tree of the object for which they were created.
作为扩展模板一部分的元素,以下称为“模板元素”,形成它们自己的逻辑树,该逻辑树与创建它们的对象的逻辑树断开连接。
Using DynamicResourceinstead of StaticResourcewill overcome the problem. However i can't tell exactly why. Maybe an explanation can be found in the Static resource lookup behaviorand Dynamic resource lookup behaviorsections in Static and Dynamic Resources, but i'm not sure.
使用DynamicResource而不是StaticResource会克服这个问题。但是我不能确切地说出原因。也许解释可以在发现静态资源查找行为和动态资源查找行为在部分静态和动态资源,但我不知道。
回答by Andrew Korell
I had a similar problem after a code cleanup. Code looked and compiled fine but would produce a "StaticResource not found error" at run time.
代码清理后我遇到了类似的问题。代码看起来和编译得很好,但在运行时会产生“未找到静态资源错误”。
Changing StaticResource to DynamicResource did work. But, it really came down to App.xaml and the order in which resource dictionaries were added under the <ResourceDictionary.MergedDictionaries>section.
将 StaticResource 更改为 DynamicResource 确实有效。但是,它确实归结为 App.xaml 以及在该<ResourceDictionary.MergedDictionaries>部分下添加资源字典的顺序。
回答by Uladzimir Sharyi
StaticResources must be defined before referring to them.
StaticResources 必须在引用它们之前定义。
Faced with the unknown static link to the resource, the XAML parser throws an exception.(This problem can be solved by using a dynamic resource, but it carries the additional costs)
面对未知的静态资源链接,XAML解析器抛出异常。(这个问题可以通过使用动态资源解决,但会带来额外的成本)

