我有一個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'.
我想我得做一些其他的方式。
這看起來很有效,它是我的一個更好的答案。 –
@MosheKatz很高興幫助 –