FrameworkElement.Resources FrameworkElement.Resources FrameworkElement.Resources FrameworkElement.Resources Property


取得或設定在本機定義的資源字典。Gets or sets the locally-defined resource dictionary.

 property System::Windows::ResourceDictionary ^ Resources { System::Windows::ResourceDictionary ^ get(); void set(System::Windows::ResourceDictionary ^ value); };
public System.Windows.ResourceDictionary Resources { get; set; }
member this.Resources : System.Windows.ResourceDictionary with get, set
Public Property Resources As ResourceDictionary


目前在本機定義的資源字典,其中每項資源都可以使用索引鍵存取。The current locally-defined dictionary of resources, where each resource can be accessed by key.


可以完全或部分中定義的資源字典Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML)通常會建立為屬性項目,和通常是在針對任何個別的頁面或應用程式的根項目。Resource dictionaries that can be defined completely or partially in Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) are typically created as a property element, and are typically on the root element for any individual page or for the application. 放置在此層級的資源字典可讓您更輕鬆地尋找從個別的子頁面中的項目 (或從任何頁面上,在應用程式的情況下)。Placing the resource dictionary at this level makes it easier to find from individual child elements in the page (or from any page, in the application case). 在大部分的應用程式案例中,我們建議樣式定義為物件項目內的資源字典中,或定義為外部的資源,以便讓整個樣式資源可彼此獨立 (這種方法有助於個別設計工具團隊的職責區隔需要加以編輯的實體檔案的開發人員責任)。In most application scenarios, we recommend that styles be defined as object elements within a resource dictionary, or be defined as external resources so that the entire style resource can be self-contained (this approach helps separate designer responsibilities from developer responsibilities by separating the physical files that need to be edited).

請注意,這個屬性會傳回該項目內直接宣告的資源字典。Note that this property returns only the resource dictionary declared directly within that element. 這是不同於實際資源查閱程序,其中的子元素可以存取任何遞迴地向上搜尋每個父項目中定義的資源。This is different than the actual resource lookup process, where a child element can access any of the resources defined in each parent element, searching recursively upwards.

資源也可以從集合中的程式碼所參考,但請注意,在建立資源XAMLXAML絕對無法供存取直到之後Loaded宣告字典項目引發。Resources can also be referenced by code from within the collection, but be aware that resources created in XAMLXAML will definitely not be accessible until after Loaded is raised by the element that declares the dictionary. 以非同步方式並不會剖析資源的事實上,甚至Loaded事件可確保您可以參考XAMLXAML定義資源。In fact, resources are parsed asynchronously and not even the Loaded event is an assurance that you can reference a XAMLXAML defined resource. 基於這個理由,您應該通常只能存取XAMLXAML定義為一部分的執行階段程式碼,或透過其他的資源XAMLXAML技術,例如樣式或屬性值的資源擴充功能參考。For this reason you should generally only access XAMLXAML defined resources as part of run-time code, or through other XAMLXAML techniques such as styles or resource extension references for attribute values. 當您透過程式碼存取資源時,它就本質上相當於DynamicResource從進行參考XAMLXAMLWhen you access resources through code, it is essentially equivalent to a DynamicResource reference made from XAMLXAML.

基礎ResourceDictionary支援加入、 移除或使用程式碼來查詢集合內的資源所需的方法。The underlying ResourceDictionary supports the methods required to add, remove or query resources from within the collection by using code. Resources屬性是可為支援的案例是完全取代的項目是一個新的或不同的資源集合ResourceDictionaryThe Resources property is settable to support the scenario of completely replacing the resources collection of an element to be a new or different ResourceDictionary.

請注意,XAMLXAML所示的語法不包括的項目ResourceDictionaryNotice that the XAMLXAML syntax shown does not include an element for the ResourceDictionary. 這是隱含的集合語法; 的範例代表集合項目的標記就可以省略。This is an example of implicit collection syntax; a tag representing the collection element can be omitted. 改為指定為項目加入至集合的項目。The elements that are added as items to the collection are specified instead. 如需有關隱含集合並XAMLXAML,請參閱 < XAML 語法詳細資料For more information about implicit collections and XAMLXAML, see XAML Syntax In Detail. 一種情況其中ResourceDictionary仍然明確指定為項目才不會導入合併的字典,在此情況下通常的任何子項目ResourceDictionaryOne case where a ResourceDictionary is still specified explicitly as an element is if you are introducing a merged dictionary, in which case there are typically no child elements for that ResourceDictionary. 如需詳細資訊,請參閱 < 合併的資源字典For details, see Merged Resource Dictionaries.

XAML 屬性項目用法XAML Property Element Usage



一或多個物件項目,其中每個定義的資源。One or more object elements, each of which defines a resource. 每個資源屬性的項目在每個ResourceDictionary必須有唯一的值X:key 指示詞,從擷取的值時做為唯一的索引鍵ResourceDictionaryEach resource property element within each ResourceDictionary must have a unique value for the x:Key Directive, which serves as the unique key when values are retrieved from the ResourceDictionary.