如何从当前组件资源中引用静态资源?

发布于 2024-09-15 15:09:07 字数 1565 浏览 3 评论 0原文

这是问题的演示:

来自一个简单的窗口:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2024-09-22 15:09:07

我认为问题在于,当它尝试查找静态资源时,它尚未合并到当前资源字典中......但不确定。

这样做似乎有效:

    <TextBlock>
        <TextBlock.Resources>    
            <ResourceDictionary>
                 <ResourceDictionary.MergedDictionaries>
                       <ResourceDictionary Source="Resources.xaml"></ResourceDictionary>
                 </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
       </TextBlock.Resources>

        <TextBlock.Text>
            <Binding  Source="{StaticResource TestString}" />
        </TextBlock.Text>           
    </TextBlock>

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:

    <TextBlock>
        <TextBlock.Resources>    
            <ResourceDictionary>
                 <ResourceDictionary.MergedDictionaries>
                       <ResourceDictionary Source="Resources.xaml"></ResourceDictionary>
                 </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
       </TextBlock.Resources>

        <TextBlock.Text>
            <Binding  Source="{StaticResource TestString}" />
        </TextBlock.Text>           
    </TextBlock>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文