2012-02-29 38 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> 

这是你在找什么?丽都/麦德龙项目模板有一个很好的例子...

相关问题