2012-06-07 56 views
6

我有一個WPF窗口應該從XAML文件加載兩個矢量圖像。 (每個文件都在一個單獨的文件中,以便在Expression Design中進行更容易的修改。)從關鍵字ResourceDictionary獲取資源

當我將XAML文件包含在MergedDictionary中時,它工作正常。 這裏是我使用的代碼:

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Images/LCCD logo.xaml" /> 
      <ResourceDictionary Source="Images/LCCD bottom image.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 

<Image Source="{Binding Source={StaticResource LCCDlogo}}" /> <!-- Simplified --> 
<Image Source="{Binding Source={StaticResource LCCDbar}}" /> <!-- Simplified --> 

不過,我現在需要添加更多的窗口資源。新資源屬於此窗口,因此我們希望它位於同一個文件中,而不是包含的文件。

當我添加<Window.Resources><ResourceDictionary>之間下面的代碼,我得到以下錯誤:

代碼

<Style TargetType="{x:Type tab:FabTabItem}"> 
     <Setter Property="Header" Value="{Binding Path=LabelText}"/> 
     <Setter Property="HeaderTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Margin="0,0,4,0"> 
         <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/> 
        </StackPanel> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

警告

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.

所以我改變<ResourceDictionary>標記爲:

<ResourceDictionary x:Key="Images"> 

但是,我不知道如何訪問此字典中的資源。如何從名爲ResourceDictionary的內部獲取資源?


編輯

沒關係。這編譯但不運行。

的錯誤是:

''Resources' property has already been set on 'MainWindow'.

我想我得做一些其他的方式。

回答

8

根據關於MergedResourceDictionary's的MSDN頁面,在MergedDictionary中指定的資源字典中定義資源是合法但不常見的。從上面的頁面。

It is legal to define resources within a ResourceDictionary that is specified as a merged dictionary, either as an alternative to specifying Source, or in addition to whatever resources are included from the specified source. However, this is not a common scenario; the main scenario for merged dictionaries is to merge resources from external file locations. If you want to specify resources within the markup for a page, you should typically define these in the main ResourceDictionary and not in the merged dictionaries.

所以試試看看它是否有效。

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Images/LCCD logo.xaml" /> 
      <ResourceDictionary Source="Images/LCCD bottom image.xaml" /> 
      <ResourceDictionary> 
       <Style TargetType="{x:Type tab:FabTabItem}"> 
        <Setter Property="Header" Value="{Binding Path=LabelText}"/> 
        <Setter Property="HeaderTemplate"> 
         <Setter.Value> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal" Margin="0,0,4,0"> 
            <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/> 
           </StackPanel> 
          </DataTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 
+0

這看起來很有效,它是我的一個更好的答案。 –

+0

@MosheKatz很高興幫助 –

1

我最終將<Style>向下移動到文件中,放到使用該樣式的元素的父元素上。

這不是最好的解決方案,但它的工作原理。