Compartilhar via


Extensão de marcação StaticResource

Provides a value for any XAML property attribute by looking up a reference to an already defined resource. Lookup behavior for that resource is analogous to load-time lookup, which will look for resources that were previously loaded from the markup of the current XAML page as well as other application sources, and will generate that resource value as the property value in the run-time objects.

XAML Attribute Usage

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

XAML Object Element Usage

<object>
  <object.property>
<StaticResource 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

Observação importanteImportante

A StaticResource must not attempt to make a forward reference to a resource that is defined lexically further within the XAML file.Não há suporte para tentar fazer isso, e mesmo se essa referência não falhe, a tentativa de referência de encaminhamento incorrerão uma penalidade de desempenho do tempo de carga quando as tabelas de hash interna que representa uma ResourceDictionary são pesquisados.For best results, adjust the composition of your resource dictionaries such that forward references can be avoided.If you cannot avoid a forward reference, use Extensão de marcação DynamicResource instead.

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

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

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

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

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

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

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

The verbose usage is often useful for extensions that have more than one settable property, or if some properties are optional. Because StaticResource 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 StaticResourceExtension classe.

StaticResource 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

Conceitos

Styling and Templating

Visão geral do XAML (WPF)

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

Visão geral sobre Recursos

Recursos e código