StaticResource 标记扩展StaticResource Markup Extension

通过查找对已定义资源的引用来为任何 XAMLXAML 属性属性提供一个值。Provides a value for any XAMLXAML property attribute by looking up a reference to an already defined resource. 该资源的查找行为与加载时查找类似,它将查找以前从当前 XAMLXAML 页的标记加载的资源以及其他应用程序源,并将该资源值生成为运行时对象中的属性值。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 XAMLXAML page as well as other application sources, and will generate that resource value as the property value in the run-time objects.

XAML 特性用法XAML Attribute Usage

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

XAML 对象元素用法XAML Object Element Usage

<StaticResource ResourceKey="key" .../>  


key 所请求资源的密钥。The key for the requested resource. 如果在标记中创建了资源,则此密钥最初由X:Key 指令分配,或在调用 ResourceDictionary.Add 时作为 key 参数提供(如果资源是在代码中创建的)。This key was initially assigned by the x:Key Directive 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.



StaticResource 不得尝试对在 XAMLXAML 文件中对其进行词法上进一步定义的资源进行前向引用。A StaticResource must not attempt to make a forward reference to a resource that is defined lexically further within the XAMLXAML file. 如果不支持这样做,则不支持此操作,即使此类引用不会失败,在搜索代表 ResourceDictionary 的内部哈希表时,尝试转发引用会导致加载时性能下降。Attempting to do so is not supported, and even if such a reference does not fail, attempting the forward reference will incur a load time performance penalty when the internal hash tables representing a ResourceDictionary are searched. 为了获得最佳结果,请调整资源字典的组合,以便可以避免转发引用。For best results, adjust the composition of your resource dictionaries such that forward references can be avoided. 如果无法避免正向引用,请改用DynamicResource 标记扩展If you cannot avoid a forward reference, use DynamicResource Markup Extension instead.

指定的 ResourceKey 应对应于现有的资源,该资源在页面、应用程序、可用控件主题和外部资源或系统资源的某一级别以X:Key 指令标识。The specified ResourceKey should correspond to an existing resource, identified with an x:Key Directive at some level in your page, application, the available control themes and external resources, or system resources. 资源查找按顺序进行。The resource lookup occurs in that order. 有关静态和动态资源的资源查找行为的详细信息,请参阅XAML 资源For more information about resource lookup behavior for static and dynamic resources, see XAML Resources.

资源键可以是XamlName 语法中定义的任意字符串。A resource key can be any string defined in the XamlName Grammar. 资源键还可以是其他对象类型,如 TypeA resource key can also be other object types, such as a Type. Type 的关键是通过隐式样式键对控件进行样式控制的基础。A Type key is fundamental to how controls can be styled by themes, through an implicit style key. 有关详细信息,请参阅控件创作概述For more information, see Control Authoring Overview.

引用资源的替代声明方法是作为DynamicResource 标记扩展The alternative declarative means of referencing a resource is as a DynamicResource Markup Extension.

特性语法是最常用于该标记扩展的语法。Attribute syntax is the most common syntax used with this markup extension. StaticResource 标识符字符串之后提供的字符串标记被指定为基础 ResourceKey 扩展类的 StaticResourceExtension 值。The string token provided after the StaticResource identifier string is assigned as the ResourceKey value of the underlying StaticResourceExtension extension class.

StaticResource 可以在对象元素语法中使用。StaticResource can be used in object element syntax. 在这种情况下,需要指定 ResourceKey 属性的值。In this case, specifying the value of the ResourceKey property is required.

StaticResource 还可以在详细特性用法中使用,以便将 ResourceKey 属性指定为一个 property=value 对: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. 由于 StaticResource 仅有一个可设置的属性,并且此属性是必需的,因此该详细用法不具有典型性。Because StaticResource has only one settable property, which is required, this verbose usage is not typical.

WPFWPF XAMLXAML 处理器实现中,对此标记扩展的处理由 StaticResourceExtension 类定义。In the WPFWPF XAMLXAML processor implementation, the handling for this markup extension is defined by the StaticResourceExtension class.

StaticResource 是标记扩展。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. XAMLXAML 中的所有标记扩展在其特性语法中都使用 { 和 } 字符,XAMLXAML 处理器通过这一约定确认标记扩展必须处理该特性。All markup extensions in XAMLXAML use the { and } characters in their attribute syntax, which is the convention by which a XAMLXAML processor recognizes that a markup extension must process the attribute. 有关详细信息,请参阅标记扩展和 WPF XAMLFor more information, see Markup Extensions and WPF XAML.

另请参阅See also