2012-02-29 35 views
13
引用在ResourceDictionary中的資源

我在我的App.xaml下面的一組代碼:從不同的ResourceDictionary在Silverlight

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml"/> 
      <ResourceDictionary Source="/Client.Common;component/Theme/Fonts.xaml"/> 
      <ResourceDictionary Source="/Client.Common;component/Theme/CoreStyles.xaml"/> 
      <ResourceDictionary Source="/Client.Common;component/Theme/SdkStyles.xaml"/> 
      <ResourceDictionary Source="/Client.Common;component/Theme/MyAppName.xaml"/> 

      <ResourceDictionary Source="/Client.Common;component/Controls/NavigationPanel.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

NavigationPanel.xaml包含了風格,看起來像這樣:

<Style x:Key="NavigationPanelListBox" TargetType="ListBox"> 
    <Setter Property="Background" Value="{StaticResource DarkBackground}" /> 
    <Lots of XAML> 
</Style> 

{StaticResource的DarkBackground}Brushes.xaml文件(即限定第一R esource字典)。它被定義爲

<SolidColorBrush x:Key="DarkBackground" Color="#FF707176" /> 

在資源字典中。

在運行時,我得到以下錯誤:

Cannot find a Resource with the Name/Key DarkBackground [Line: 16 Position: 44] 

行號和位置引用NavigationPanel.xaml資源字典在App.xaml中。

我可以引用其他控件的筆刷,而不是包含的資源字典。

爲什麼我不能引用或爲什麼它不解析對合並資源字典的層次更高的資源的引用?我在這裏錯過了什麼?

回答

13

你是否參考刷在NavigationPanel字典中的任何資源?

如果您發現該內容需要與Brushes資源字典集結合起來,請致電NavigationPanel

於是在NavigationPanel字典中。

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml" /> 
</ResourceDictionary.MergedDictionaries> 
+1

這是行不通的,我明白爲什麼現在可以解決,但爲什麼它不在更高級別的字典中解決?我的答案是正確的,但如果你知道這是爲什麼,我會很樂意解釋。 – 2012-02-29 20:20:39

+0

在另一個線程發現了這一點。留在這裏爲後人。 https://social.msdn.microsoft.com/forums/windowsapps/en-US/2be9a5f6-5313-448d-a9d9-296bac42215e/using-style-defined-in-merged-dictionary-from-another-merged-dictionary?論壇= wpdevelop感謝卡米爾Nieweglowski爲此。 – Snouto 2015-05-15 10:51:28

8

您可以在另外一個字典(像在C# '使用'),像這樣:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
    xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:Controls="clr-namespace:APC.IKM.UI.SL.Controls" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> 

    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="Brushes.xaml"/> 
     <ResourceDictionary Source="Fonts.xaml"/> 
     <ResourceDictionary Source="CoreStyles.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 

這是你在找什麼?麗都/麥德龍項目模板有一個很好的例子...

相關問題