Extensão de marcação DynamicResource

Provides a value for any XAML property attribute by deferring that value to be a reference to a defined resource. Lookup behavior for that resource is analogous to run-time lookup.

XAML Attribute Usage

<object property="{DynamicResource key}" .../>

XAML Property Element Usage

<object>
  <object.property>
    <DynamicResource ResourceKey="key" .../>
  </object.property>
</object>

XAML Values

key

The key for the requested resource. This key was initially assigned by the Diretiva X:Key if a resource was created in markup, or was provided as the key parameter when calling ResourceDictionary.Add if the resource was created in code.

Comentários

A DynamicResource will create a temporary expression during the initial compilation and thus defer lookup for resources until the requested resource value is actually required in order to construct an object. This may potentially be after the XAML page is loaded. The resource value will be found based on key search against all active resource dictionaries starting from the current page scope, and is substituted for the placeholder expression from compilation.

Observação importanteImportante

In terms of dependency property precedence, a DynamicResource expression is equivalent to the position where the dynamic resource reference is applied.If you set a local value for a property that previously had a DynamicResource expression as the local value, the DynamicResource is completely removed.For details, see Precedência de valores de propriedade de dependência.

Certain resource access scenarios are particularly appropriate for DynamicResource as opposed to a Extensão de marcação StaticResource. See Visão geral sobre Recursos for a discussion about the relative merits and performance implications of DynamicResource and StaticResource.

The specified ResourceKey should correspond to an existing resource determined by Diretiva X:Key at some level in your page, application, the available control themes and external resources, or system resources, and the resource lookup will happen in that order. For more information about resource lookup for static and dynamic resources, see Visão geral sobre Recursos.

A resource key may be any string defined in the Gramática XamlName. A resource key may also be other object types, such as a Type. A Type key is fundamental to how controls can be styled by themes. For more information, see Visão geral sobre criação de controles.

APIspara a pesquisa de valores de recursos, como FindResource, execute a mesma lógica de pesquisa de recurso conforme usado por DynamicResource.

The alternative declarative means of referencing a resource is as a Extensão de marcação StaticResource.

Attribute syntax is the most common syntax used with this markup extension. The string token provided after the DynamicResource identifier string is assigned as the ResourceKey value of the underlying DynamicResourceExtension extension class.

DynamicResource can be used in object element syntax. In this case, specifying the value of the ResourceKey property is required.

DynamicResource can also be used in a verbose attribute usage that specifies the ResourceKey property as a property=value pair:

<object property="{DynamicResource ResourceKey=key}" .../>

The verbose usage is often useful for extensions that have more than one settable property, or if some properties are optional. Because DynamicResource has only one settable property, which is required, this verbose usage is not typical.

No WPF XAML implementação do processador, a manipulação desta extensão de marcação é definida pelo DynamicResourceExtension classe.

DynamicResource is a markup extension. Markup extensions are typically implemented when there is a requirement to escape attribute values to be other than literal values or handler names, and the requirement is more global than just putting type converters on certain types or properties. Todas as extensões de marcação no XAML usar o {e} caracteres em sua sintaxe de atributo, que é a convenção pelo qual um XAML processador reconhece que a extensão de marcação deve processar o atributo. For more information, see As extensões de marcação e o WPF XAML.

Consulte também

Referência

Diretiva X:Key

Extensão de marcação StaticResource

Conceitos

Visão geral sobre Recursos

Recursos e código

Visão geral do XAML (WPF)

As extensões de marcação e o WPF XAML

As extensões de marcação e o WPF XAML