Referencing a resource in a ResourceDictionary from a different ResourceDictionary in Silverlight

半腔热情 提交于 2019-12-03 16:24:44

问题


I have the following set of code in my App.xaml:

<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>

The NavigationPanel.xaml contains a style that looks like this:

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

The {StaticResource DarkBackground} is defined in the Brushes.xaml file (i.e. the first resource dictionary). It is defined as

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

in the resource dictionary.

At runtime, I get the following error:

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

The line numbers and position references the NavigationPanel.xaml resource dictionary in the app.xaml.

I can reference the brush from other controls, just not the included resource dictionary.

Why can I not reference or why does it not resolve the reference to a resource that is higher in the heirarchy of the merged resource dictionary?? What am I missing here?


回答1:


Are you referencing the DarkBackground brush in any of the resources in the NavigationPanel dictionary?

If you are you might need to merge the Brushes resource dictionary into the NavigationPanel dictionary.

So in the NavigationPanel dictionary.

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



回答2:


You can include one dictionary in another (like 'using' in C#) like so:

<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>

Is this what you are looking for? The Cosmopolitan / Metro project template has a good example of this...




回答3:


the trully answer is Eric's answer in this site: https://social.msdn.microsoft.com/forums/windowsapps/en-US/2be9a5f6-5313-448d-a9d9-296bac42215e/using-style-defined-in-merged-dictionary-from-another-merged-dictionary?forum=wpdevelop.
the Brushes.xaml and the NavigationPanel.xaml is parsed independently and then added to the merged dictionary of Application resources so they don't know anything about each other.



来源:https://stackoverflow.com/questions/9494729/referencing-a-resource-in-a-resourcedictionary-from-a-different-resourcedictiona

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!