首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >X:键在MergedDictionaries中被忽略

X:键在MergedDictionaries中被忽略
EN

Stack Overflow用户
提问于 2015-09-10 00:38:13
回答 1查看 352关注 0票数 2

我有两种风格。一个具有隐式键(通过TargetType属性设置),另一个具有显式键:

代码语言:javascript
复制
<Application x:Class="WpfTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="Button" x:Key="MyButton">

            </Style>
            <Style TargetType="Button">

            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

一切正常是预料之中的。现在,如果在MergedDictionaries中定义了相同的样式,如下所示:

代码语言:javascript
复制
<Application x:Class="WpfTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <Style TargetType="Button" x:Key="MyButton">

                    </Style>
                    <Style TargetType="Button">

                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

我得到一个运行时异常,说明我有重复的键:

'System.Windows.ResourceDictionary.DeferrableContent‘System.Windows.Markup.XamlParseException:“Set属性System.Windows.Markup.XamlParseException抛出了一个例外。”行号'14‘和线位置'19’。-> System.ArgumentException:项已经添加了。字典中的键:'System.Windows.Controls.Button‘键被添加:'System.Windows.Controls.Button’

这没什么意义。看起来x:KeyMergedDictionaries中被忽略了。为什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-10 00:38:13

看起来这是XAML中的一个bug。如果我将x:Key移动为第一个属性(在TargetType之前),那么所有东西都会突然正常工作。

代码语言:javascript
复制
<Application x:Class="WpfTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <Style x:Key="MyButton" TargetType="Button">

                    </Style>
                    <Style TargetType="Button">

                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

编辑: 连接上的错误

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32491234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档