2010-08-06 60 views
23

,我发现了以下错误:XAML:属性 '资源' 设置不止一次

The property 'Resources' is set more than once.

这是我的XAML:

<UserControl.Resources> 
    <!--Resource dictionaries for framework stuff--> 
    <ResourceDictionary> 
     <Style x:Key="MultiLineTextBox" TargetType="TextBox"> 
      <Setter Property="BorderThickness" Value="0"/> 
      <Setter Property="TextWrapping" Value="WrapWithOverflow"/> 
     </Style> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/View;component/Common/ResourceDictionary.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 

    <!--Convertors needed for proper display--> 
    <c:CollapsedIfNegative x:Key="CollapseIfNegative"/> 
    <c:VisibleIfNegative x:Key="MakeVisibleIfNegative"/> 
    <c:ErrorCodeToString x:Key="ConvertErrorCodeToString"/> 
</UserControl.Resources> 

回答

47

.Resources财产XAML是聪明的:它的类型ResourceDictionary,但如果你没有明确把<ResourceDictionary>标签围绕它的内容,编译器会神奇地假设一个给你。这就是为什么你通常可以把你的画笔直接加入标记。

但是,您已经开始放入自己的ResourceDictionary - 我怀疑它已经阻止了这种自动行为 - 因此编译器现在认为您正试图设置多个值。如果您重写这样你应该得到你之后的结果:

<UserControl.Resources> 
    <!--Resource dictionaries for framework stuff--> 
    <ResourceDictionary> 
     <!--Convertors needed for proper display--> 
     <!-- move this INSIDE the ResourceDictionary tag --> 
     <c:CollapsedIfNegative x:Key="CollapseIfNegative"/> 
     <c:VisibleIfNegative x:Key="MakeVisibleIfNegative"/> 
     <c:ErrorCodeToString x:Key="ConvertErrorCodeToString"/> 


     <Style x:Key="MultiLineTextBox" TargetType="TextBox"> 
      <Setter Property="BorderThickness" Value="0"/> 
      <Setter Property="TextWrapping" Value="WrapWithOverflow"/> 
     </Style> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/View;component/Common/ResourceDictionary.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</UserControl.Resources> 
+0

这有效,但我不明白为什么它需要在中的最后一个元素转换器的问题,但不会成为第一个问题。 – 2010-08-06 19:07:10

+1

在你的例子中,它们根本不在'ResourceDictionary'中。我认为'MergedDictionaries'元素必须是第一个或最后一个,但除此之外,顺序并不重要。 – 2010-08-06 19:33:44

+3

这一句话值得黄金分享:“如果你没有明确地在它的内容上放置一个标签,编译器会神奇地为你呈现一个” - 非常感谢你。 – 2015-09-14 03:11:46

0

其实,复制你的XAML并将其粘贴在我自己的UserControl构建就好(只要我添加引用的转换器类)。

您是否在错误列表中看到任何其他错误,或者这是唯一的错误?有时,如果发生另一个错误(例如找不到资源),可能会导致发生另一个编译错误。

+0

错误属性元素不能在元素内容的中间。他们必须在内容之前或之后。 – 2010-08-06 16:28:11