共用方式為


ResourceDictionary.MergedDictionaries Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a collection of the ResourceDictionary dictionaries that constitute the various resource dictionaries in the merged dictionaries.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public ReadOnly Property MergedDictionaries As PresentationFrameworkCollection(Of ResourceDictionary)
public PresentationFrameworkCollection<ResourceDictionary> MergedDictionaries { get; }
<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
    oneOrMoreResourceDictionaryElements
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

XAML Values

Property Value

Type: System.Windows.PresentationFrameworkCollection<ResourceDictionary>
A collection of the ResourceDictionary dictionaries that constitute the various resource dictionaries in the merged dictionaries.

Remarks

Dictionaries are merged by adding a ResourceDictionary to the generic collection referenced by MergedDictionaries. A merged ResourceDictionary does not have resource elements defined within it in markup. Specifying resource elements within a ResourceDictionary that also has Source specified is not supported. Instead, the merged dictionary is a ResourceDictionary with no markup child elements defined (or with no elements added through code), but with a URI specified for Source. The Source specified as a URI enables the merged dictionary to come from an external source, such as "loose XAML" that is packaged with the XAP by the Content build action but not compiled into the DLL. When used as a merged dictionary source, it is illegal for a XAML file that has a ResourceDictionary root to specify x:Class.

Lookup behavior within the MergedDictionaries collection searches the last-added ResourceDictionary first, and the search stops as soon as a requested key is found. In other words, the retrieval logic from the collection of merged resource dictionaries is last in, first out.

Duplicate keys across the collection of merged dictionaries are not illegal. In fact, you can use multiple merged ResourceDictionary sources to implement a deliberate fallback sequence, where the last-in-sequence ResourceDictionary contains the fallbacks in case no preceding ResourceDictionary defined the key.

Merged Dictionary Lookup

Resources in a merged dictionary occupy a location in the resource lookup scope that is just after the scope of the main resource dictionary they are merged into. A resource key must be unique within any individual dictionary (including within any individual ResourceDictionary that is the source for one of the merged dictionaries). However, a key can exist multiple times in the combined set of merged dictionaries. In this case, the resource that is returned will come from the last dictionary found sequentially in the MergedDictionaries collection.

For example, consider the following XAML:

<UserControl.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="rd1.xaml"/>
      <ResourceDictionary Source="rd2.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</UserControl.Resources>

<!--rd1.xaml, xmlns omitted-->
<ResourceDictionary>
  <SolidColorBrush x:Key="maindialogbrush" Color="Orange"/>
</ResourceDictionary>

<!--rd2.xaml, xmlns omitted-->
<ResourceDictionary>
  <SolidColorBrush x:Key="maindialogbrush" Color="LimeGreen"/>
</ResourceDictionary>

In this example, if a resource request is made for the resource with the key maindialogbrush, the returned brush will be the color LimeGreen, because rd2.xaml is the last defined merged dictionary.

If a key is defined in the primary dictionary and also in one or more MergedDictionaries collection dictionaries, then the resource that is returned will come from the primary dictionary. In other words, the primary dictionary overrides any considerations of the lookup order between multiple merged dictionaries in the same MergedDictionaries set.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.