2010-08-06 41 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