无法将具有相同键的元素添加到资源字典 - 它已经存在
Cannot add element with the same key to resource dictionary - it already exists
在解析一个 xaml 文件时出现异常:
Result Message:
Test method ThemeResourceDictionaryTest.ParseXaml threw exception:
System.Windows.Markup.XamlParseException: 'Add value to dictionary of type 'System.Windows.ResourceDictionary' threw an exception.' Line number '1397' and line position '4'. ---> System.ArgumentException: Item has already been added. Key in dictionary: 'System.Windows.Controls.TextBox' Key being added: 'System.Windows.Controls.TextBox'
和我的元素:
<Style x:Key="MyVeryUniqueKey" BasedOn="{StaticResource TextBoxStyle}" TargetType="TextBox">
<Setter Property="MinWidth" Value="90"/>
</Style>
为什么元素的键设置为TextBox,当我明确设置键为MyVeryUniqueKey时,它是唯一的。
我还有其他样式,其目标类型是 TextBox,但它们都有不同的键。
我怀疑,在特定的资源字典范围内,您有不止一种 TextBox
样式,其中设置了 TargetType
但未设置 x:Key
。
如果您没有为 TextBox
样式显式设置字典键,它将默认为 {x:Type TextBox}
。您显然不能将其中两个复制为字典中的键。
比如你把这种东西放在资源里:
<Window.Resources>
<Style TargetType="{x:Type TextBox}" />
<Style TargetType="{x:Type TextBox}" />
</Window.Resources>
然后你会得到一个runtime XamlParseException
:
Item has already been added.
Key in dictionary: 'System.Windows.Controls.TextBox'
Key being added: 'System.Windows.Controls.TextBox'"
我在使用不同键的两种样式时遇到了同样的错误。
x:Key 必须在 TargetType 前面。我还是不敢相信...
作品:
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> ...
<Style x:Key="ButtonStyle2" TargetType="{x:Type Button}"> ...
无效:
<Style TargetType="{x:Type Button}" x:Key="ButtonStyle"> ...
<Style TargetType="{x:Type Button}" x:Key="ButtonStyle2"> ...
在解析一个 xaml 文件时出现异常:
Result Message:
Test method ThemeResourceDictionaryTest.ParseXaml threw exception: System.Windows.Markup.XamlParseException: 'Add value to dictionary of type 'System.Windows.ResourceDictionary' threw an exception.' Line number '1397' and line position '4'. ---> System.ArgumentException: Item has already been added. Key in dictionary: 'System.Windows.Controls.TextBox' Key being added: 'System.Windows.Controls.TextBox'
和我的元素:
<Style x:Key="MyVeryUniqueKey" BasedOn="{StaticResource TextBoxStyle}" TargetType="TextBox">
<Setter Property="MinWidth" Value="90"/>
</Style>
为什么元素的键设置为TextBox,当我明确设置键为MyVeryUniqueKey时,它是唯一的。
我还有其他样式,其目标类型是 TextBox,但它们都有不同的键。
我怀疑,在特定的资源字典范围内,您有不止一种 TextBox
样式,其中设置了 TargetType
但未设置 x:Key
。
如果您没有为 TextBox
样式显式设置字典键,它将默认为 {x:Type TextBox}
。您显然不能将其中两个复制为字典中的键。
比如你把这种东西放在资源里:
<Window.Resources>
<Style TargetType="{x:Type TextBox}" />
<Style TargetType="{x:Type TextBox}" />
</Window.Resources>
然后你会得到一个runtime XamlParseException
:
Item has already been added. Key in dictionary: 'System.Windows.Controls.TextBox' Key being added: 'System.Windows.Controls.TextBox'"
我在使用不同键的两种样式时遇到了同样的错误。
x:Key 必须在 TargetType 前面。我还是不敢相信...
作品:
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> ...
<Style x:Key="ButtonStyle2" TargetType="{x:Type Button}"> ...
无效:
<Style TargetType="{x:Type Button}" x:Key="ButtonStyle"> ...
<Style TargetType="{x:Type Button}" x:Key="ButtonStyle2"> ...