Dicionários de Recursos mesclados
Windows Presentation Foundation (WPF) resources support a merged resource dictionary feature. Esse recurso fornece uma maneira de definir a parte de recursos de um WPF aplicativo fora do compilado XAML aplicativo. Resources can then be shared across applications and are also more conveniently isolated for localization.
Introducing a Merged Resource Dictionary
In markup, you use the following syntax to introduce a merged resource dictionary into a page:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="myresourcedictionary.xaml"/>
<ResourceDictionary Source="myresourcedictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
Note that the ResourceDictionary element does not have an Diretiva X:Key, which is generally required for all items in a resource collection. But another ResourceDictionary reference within the MergedDictionaries collection is a special case, reserved for this merged resource dictionary scenario. The ResourceDictionary that introduces a merged resource dictionary cannot have an Diretiva X:Key. Typically, each ResourceDictionary within the MergedDictionaries collection specifies a Source attribute. The value of Source should be a uniform resource identifier (URI) that resolves to the location of the resources file to be merged. The destination of that URI must be another XAML file, with ResourceDictionary as its root element.
Observação
It is legal to define resources within a ResourceDictionary that is specified as a merged dictionary, either as an alternative to specifying Source, or in addition to whatever resources are included from the specified source.However, this is not a common scenario; the main scenario for merged dictionaries is to merge resources from external file locations.If you want to specify resources within the markup for a page, you should typically define these in the main ResourceDictionary and not in the merged dictionaries.
Merged Dictionary Behavior
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. Although a resource key must be unique within any individual dictionary, a key can exist multiple times in a set of merged dictionaries. In this case, the resource that is returned will come from the last dictionary found sequentially in the MergedDictionaries collection. If the MergedDictionaries collection was defined in XAML, then the order of the merged dictionaries in the collection is the order of the elements as provided in the markup. If a key is defined in the primary dictionary and also in a dictionary that was merged, then the resource that is returned will come from the primary dictionary. These scoping rules apply equally for both static resource references and dynamic resource references.
Merged Dictionaries and Code
Merged dictionaries can be added to a Resources dictionary through code. The default, initially empty ResourceDictionary that exists for any Resources property also has a default, initially empty MergedDictionaries collection property. To add a merged dictionary through code, you obtain a reference to the desired primary ResourceDictionary, get its MergedDictionaries property value, and call Add on the generic Collection that is contained in MergedDictionaries. The object you add must be a new ResourceDictionary. In code, you do not set the Source property. Instead, you must obtain a ResourceDictionary object by either creating one or loading one. Uma forma de carregar um existente ResourceDictionary para chamar XamlReader.Load em uma XAML fluxo de arquivo que tenha um ResourceDictionary raiz, em seguida, a projeção do XamlReader.Load retornar o valor para ResourceDictionary.
Merged Resource Dictionary URIs
Existem várias técnicas para saber como incluir um dicionário de recurso mescladas, que são indicadas pela formato deuniform resource identifier (URI) que você usará. Falando genericamente, essas técnicas podem ser divididas em duas categorias: os recursos são compilados como parte do projeto e recursos que não são compilados como parte do projeto.
For resources that are compiled as part of the project, you can use a relative path that refers to the resource location. The relative path is evaluated during compilation. Your resource must be defined as part of the project as a Resource build action. If you include a resource .xaml file in the project as Resource, you do not need to copy the resource file to the output directory, the resource is already included within the compiled application. You can also use Content build action, but you must then copy the files to the output directory and also deploy the resource files in the same path relationship to the executable.
Observação
Do not use the Embedded Resource build action.The build action itself is supported for WPF applications, but the resolution of Source does not incorporate ResourceManager, and thus cannot separate the individual resource out of the stream.Você ainda pode usar o recurso incorporado para outros fins, desde que você também usou ResourceManager para acessar recursos.
A related technique is to use a Pack URI to a XAML file, and refer to it as Source. O Pack URI permite referências a componentes de assemblies referenciados e outras técnicas. Para obter mais informações sobre o pacote de URIs, consulte Recursos, conteúdo e arquivos de dados de aplicativos WPF.
For resources that are not compiled as part of the project, the URI is evaluated at run time. Você pode usar um transporte URI comuns como, por exemplo, arquivo: ou http: para referir-se o arquivo de recurso. A desvantagem de usar a abordagem de recursos não compiladas é esse arquivo: acesso requer etapas adicionais de implantação e o http: acesso implica a zona de segurança da Internet.
Reusing Merged Dictionaries
You can reuse or share merged resource dictionaries between applications, because the resource dictionary to merge can be referenced through any valid uniform resource identifier (URI). Exactly how you do this will depend on your application deployment strategy and which application model you follow. The aforementioned Pack URI strategy provides a way to commonly source a merged resource across multiple projects during development by sharing an assembly reference. Neste cenário os recursos ainda são distribuídos pelo cliente e pelo menos um dos aplicativos deve implantar o assembly referenciado. Também é possível referenciar recursos mesclados por meio de um URI distribuído que usa o protocolo http.
Writing merged dictionaries as local application files or to local shared storage is another possible merged dictionary / application deployment scenario.
Localization
If resources that need to be localized are isolated to dictionaries that are merged into primary dictionaries, and kept as loose XAML, these files can be localized separately. This technique is a lightweight alternative to localizing the satellite resource assemblies. For details, see Visão geral de globalização e localização do WPF.