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 Attribute UsageXAML Attribute Usage

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

XAML 屬性項目用法XAML Property Element Usage

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


key 要求資源的金鑰。The key for the requested resource. 如果資源是在標記中建立,則此索引鍵一開始是由x:Key指示詞所指派, key或在呼叫ResourceDictionary.Add時當做參數提供(如果資源是在程式碼中建立)。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. 如需DynamicResourceStaticResource的相對優勢和效能含意討論,請參閱XAML 資源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.

用於查閱資源值(例如FindResource)的 api 會遵循所使用的相同資源查閱邏輯。 DynamicResourceAPIs 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) 語法是最常搭配這個標記延伸來使用的語法。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) 做為 property=value 配對組的詳細屬性 (Attribute) 使用方式中。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. 如果必須將屬性 (Attribute) 值加上逸出符號,以免成為常值或處理常式名稱,而且這個動作必須更全面地實施 (而不是只對特定類型或屬性 (Property) 設定類型轉換子 (Type Converter)),則通常會實作標記延伸。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