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.


ADynamicResource會建立初始的編譯期間的暫時運算式,並因此延後資源的查閱,直到實際需要以建構物件,而要求的資源值。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 資源如需有關的效能含意與相對優點的討論DynamicResourceStaticResourceSee 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. AType金鑰是由佈景主題樣式的控制項可以為何的基礎。A Type key is fundamental to how controls can be styled by themes. 如需詳細資訊,請參閱控制項撰寫概觀For more information, see Control Authoring Overview.

APIAPIs 查閱的資源值,例如FindResource,請依照下列所用的相同的資源查閱邏輯DynamicResourcefor 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