如何从当前组件资源中引用静态资源?
这是问题的演示:
来自一个简单的窗口:
<Window x:Class="TestWpfStaticResource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<TextBlock Text="{StaticResource TestString}">
<TextBlock.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</TextBlock.Resources>
</TextBlock>
我正在尝试访问 ResourceDictionary 中的资源:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="TestString">Test</sys:String>
</ResourceDictionary>
Visual Studio 2010 显示了带有预期结果的窗口,但在运行应用程序时出现此异常:
Cannot find resource named 'TestString'. Resource names are case sensitive.
注意 1:使用 DynamicResource 时,结果在 VS 2010 和运行时都很好。
注 2:将对资源字典的引用放在 TextBlock 之外,例如在 Windows 资源中,给出了预期的结果,但我的实际场景不允许这样做。
那么,我做错了什么以及如何获得预期的结果?
答案:经过更多测试,XAML 解析器似乎非常小,并且会解析内容,按顺序进行,而不尝试解释任何内容。 因此声明顺序很重要:您必须将对“Text”属性的引用放在对字典的引用之后。 唯一的方法似乎是将 StaticResource 的引用包装在“Binding”元素中。
提前致谢。
here is a demonstration of the problem :
from a simple window :
<Window x:Class="TestWpfStaticResource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<TextBlock Text="{StaticResource TestString}">
<TextBlock.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</TextBlock.Resources>
</TextBlock>
I'm trying to access a resource in a ResourceDictionary :
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="TestString">Test</sys:String>
</ResourceDictionary>
Visual Studio 2010 shows my window with the expected result but when running the application I obtain this exception :
Cannot find resource named 'TestString'. Resource names are case sensitive.
Note 1 : when using a DynamicResource the result is fine in VS 2010 and at runtime.
Note 2 : putting the reference to the resource dictionary outside of the TextBlock, in the Windows Resources for instance, gives the expected result but my real scenario does not allow that.
So, what am I doing wrong and how to obtain the expected result ?
ANSWER : after more testing it appears that the XAML parser is quite minimal and parses content as it comes, sequentially, without trying to interpret anything.
So declaration order is important : you have to put the reference to the "Text" property AFTER the reference to the dictionary.
And the only way to do that seems to wrap the reference to the StaticResource in a "Binding" element.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于,当它尝试查找静态资源时,它尚未合并到当前资源字典中......但不确定。
这样做似乎有效:
I think the problem is that when it tries to find the static resource, it hasn't yet been merged into the current resource dictionary... not sure though.
Doing it like this seems to work: