如何在 SL4 页面中使用键定义资源和 MergeDictionary

发布于 2024-09-25 04:31:32 字数 1527 浏览 1 评论 0原文

这可能是一个非常愚蠢的问题,但我无法弄清楚。

我有一个定义了 MergeDictionary 的页面:

  <navigation:Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </navigation:Page.Resources>

并且我在 XAML 中引用了 TourneySetupStyles.xaml 中的样式,这样没有问题:

<TextBlock Text="Tourney Name:" Style="{StaticResource TourneySetupTextStyle}" />

但是,现在我需要添加另一个页面资源,如下所示:

但设计师现在发出警告: “设计器不支持加载混合没有键的“ResourceDictionary”项目和同一集合中的其他项目的词典。请确保“Resources”属性不包含没有键的“ResourceDictionary”项目,或者“ResourceDictionary” item 是集合中唯一的元素。”

因此,我向我的 ResourceDictionary 添加了一个键,如下所示:

   <navigation:Page.Resources>
         <local:Tournament x:Key="tournament" />
        <ResourceDictionary x:Key="whatever">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </navigation:Page.Resources>

然后警告就消失了。但现在我对 TourneySetupStyles 中样式的引用不再起作用:

“找不到具有名称/密钥 TourneySetupTextStyle 的资源”

所以我想问题是:既然 ResourceDictionary 已被键入,我如何访问样式?

This is probably a really stupid question but I can't figure this out.

I have a page with a MergeDictionary defined:

  <navigation:Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </navigation:Page.Resources>

and I reference the styles in TourneySetupStyles.xaml in my XAML like this with no problem:

<TextBlock Text="Tourney Name:" Style="{StaticResource TourneySetupTextStyle}" />

However, now I need to add another page resource like this:

But the designer now throws a warning:
"The designer does not support loading dictionaries that mix 'ResourceDictionary' items without a key and other items in the same collection. Please ensure that the 'Resources' property does not contain 'ResourceDictionary' items without a key, or that the 'ResourceDictionary' item is the only element in the collection."

So I add a key to my ResourceDictionary like this:

   <navigation:Page.Resources>
         <local:Tournament x:Key="tournament" />
        <ResourceDictionary x:Key="whatever">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </navigation:Page.Resources>

and the warning goes away. BUT now my reference to the style in TourneySetupStyles no longer works:

"Cannot find a Resource with the Name/Key TourneySetupTextStyle"

So I guess the question is: How do I access the style now that the ResourceDictionary is keyed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

空心↖ 2024-10-02 04:31:32

我今天刚刚遇到这个问题——我正在交叉编译到 WPF / Silverlight。
将本地资源放在ResourceDictionary节点中,不要将ax:Key放在ResourceDictionary节点中。

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/mydll;component/folder/MyResDict.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <LinearGradientBrush x:Key="OrangeGradient"
                             EndPoint="0.5,1"
                             MappingMode="RelativeToBoundingBox"
                             StartPoint="0.5,0">
            <LinearGradientBrush.RelativeTransform>
                <RotateTransform  CenterY="0.5"
                                  CenterX="0.5" />
            </LinearGradientBrush.RelativeTransform>
            <GradientStop Color="#FFF3801E" />
            <GradientStop Color="#FFEDB17E"
                          Offset="0.5" />
            <GradientStop Color="#FFF3801E"
                          Offset="1" />
        </LinearGradientBrush>
    </ResourceDictionary>
</UserControl.Resources>

我无法解释为什么 - 但我知道它有效......

hth

I just ran into this today -- I'm cross compiling to WPF / Silverlight.
Put the local resource inside the ResourceDictionary node, don't put a x:Key on the ResourceDictionary node.

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/mydll;component/folder/MyResDict.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <LinearGradientBrush x:Key="OrangeGradient"
                             EndPoint="0.5,1"
                             MappingMode="RelativeToBoundingBox"
                             StartPoint="0.5,0">
            <LinearGradientBrush.RelativeTransform>
                <RotateTransform  CenterY="0.5"
                                  CenterX="0.5" />
            </LinearGradientBrush.RelativeTransform>
            <GradientStop Color="#FFF3801E" />
            <GradientStop Color="#FFEDB17E"
                          Offset="0.5" />
            <GradientStop Color="#FFF3801E"
                          Offset="1" />
        </LinearGradientBrush>
    </ResourceDictionary>
</UserControl.Resources>

I can't explain why - but I know it works...

hth

ζ澈沫 2024-10-02 04:31:32

叹息似乎声明的顺序很重要,一旦我将第一个资源向下移动,它现在就可以工作了:

<navigation:Page.Resources>
    <ResourceDictionary x:Key="TourneySetupStyles">
            <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    <local:Tournament x:Key="tourneySetupViewModel" />
</navigation:Page.Resources>

如果有人可以解释为什么它对于将来的参考会很好......

sigh it seems that the order of the declarations is important, as soon as I move the first resource down it now works:

<navigation:Page.Resources>
    <ResourceDictionary x:Key="TourneySetupStyles">
            <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    <local:Tournament x:Key="tourneySetupViewModel" />
</navigation:Page.Resources>

If anyone can explain why it would be great for future reference...

音盲 2024-10-02 04:31:32

我遇到了同样的问题。

我解决了在应用程序的 xaml 文件中定义字典合并而不是视图本身的问题。

前任:

    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources\Brushes\Brushes_Dictionary.xaml" />
            <ResourceDictionary Source="Resources\Storyboards\Storyboard_Dictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>

</Application.Resources>

I came across the same problem.

I resolved the issue be defining my dictionary merges within the xaml file of the application instead of the view itself.

Ex:

    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources\Brushes\Brushes_Dictionary.xaml" />
            <ResourceDictionary Source="Resources\Storyboards\Storyboard_Dictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>

</Application.Resources>

趴在窗边数星星i 2024-10-02 04:31:32

是的,我刚刚被这个咬过。一旦 Silverlight 加载合并的资源字典,它就会删除您已添加的所有本地资源!就我而言,我需要在 InitalizeComponent() 调用之前以编程方式添加资源,但由于 UserControl 包含合并的 ResourceDictionary,因此该资源会丢失。恕我直言,这是 Silverlight 中的一个错误。

但是将本地资源放在 ResourceDictionary 之后将适用于像您这样的情况,所以我已经投票了。

Yes, I've just been bitten by this. As soon as Silverlight loads the merged resource dictionary, it deletes all local resources you've already added! In my case I need to programmatically add a resource before the InitalizeComponent() call, but since the UserControl includes a merged ResourceDictionary that resource is lost. IMHO this is a bug in Silverlight.

But putting local resources after the ResourceDictionary will work for cases like yours so I've up voted it.

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