DynamicResource 标记扩展DynamicResource Markup Extension

通过将值推迟为对已定义资源的引用来为任何 XAMLXAML 属性特性提供一个值。Provides a value for any XAMLXAML 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 特性用法XAML Attribute Usage

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

XAML 属性元素用法XAML Property Element Usage

    <DynamicResource 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.


DynamicResource 将在初始编译期间创建一个临时表达式,因而延迟对资源的查找,直到实际需要请求的资源值才能构造对象。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. 这可能在加载 XAMLXAML 页面之后。This may potentially be after the XAMLXAML 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.


就依赖属性优先级而言,DynamicResource 表达式等效于应用动态资源引用的位置。In terms of dependency property precedence, a DynamicResource expression is equivalent to the position where the dynamic resource reference is applied. 如果为先前具有 DynamicResource 表达式作为本地值的属性设置本地值,则 DynamicResource 将被完全删除。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 Dependency Property Value Precedence.

某些资源访问方案特别适用于 DynamicResource 而不是StaticResource 标记扩展Certain resource access scenarios are particularly appropriate for DynamicResource as opposed to a StaticResource Markup Extension. 请参阅XAML 资源,以了解有关 DynamicResourceStaticResource相对的优点和性能影响的讨论。See XAML Resources for a discussion about the relative merits and performance implications of DynamicResource and StaticResource.

指定的 ResourceKey 应对应于页面、应用程序、可用控件主题和外部资源或系统资源的某个级别由X:Key 指令确定的现有资源,并按该顺序进行资源查找。The specified ResourceKey should correspond to an existing resource determined by x:Key Directive 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. 有关静态和动态资源的资源查找的详细信息,请参阅XAML 资源For more information about resource lookup for static and dynamic resources, see XAML Resources.

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

用于查找资源值的 Api (如 FindResource)遵循 DynamicResource所使用的相同资源查找逻辑。APIs for lookup of resource values, such as FindResource, follow the same resource lookup logic as used by DynamicResource.

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

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

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

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

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

DynamicResource 是标记扩展。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. 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