FrameworkContentElement.Resources 屬性


取得或設定目前本機定義的資源字典。Gets or sets the current 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 resources. 這是資源的字典,字典內的資源是依索引鍵來存取。This is a dictionary of resources, where resources within the dictionary are accessed by key.



下列範例會在 FlowDocument 根項目上建立 Resources 集合。The following example establishes a Resources collection on a FlowDocument root element. FlowDocument 是一般的選擇,因為它是幾個可做為根項目的 FrameworkContentElement 類別之一,而資源通常會儲存在頁面根目錄或甚至更高的層級,例如應用程式中。FlowDocument is a typical choice because it is one of the few FrameworkContentElement classes that make sense as a root element, and resources are generally stored at the page root or at even higher levels such as in the application.

  x:Class=" SDKSample.Page2">
    <Style TargetType="{x:Type Paragraph}" x:Key="BluePara">
      <Setter Property="Background" Value="Blue"/>
      <Setter Property="FontSize" Value="18"/>
      <Setter Property="Foreground" Value="LightBlue"/>
      <Setter Property="FontFamily" Value="Trebuchet MS"/>
  <Paragraph Style="{StaticResource BluePara}">Lorem ipsum etc.</Paragraph>


可以完全或部分在 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. 當您透過程式碼存取資源時,基本上等同于 XAMLXAML所做的DynamicResource參考。When 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 屬性可設定為支援將元素的 resources 集合完全取代為新的或不同 ResourceDictionary的案例。The 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 語法不包含 ResourceDictionary的元素。Notice 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 做為專案,就是如果您引進合併的字典,在這種情況下,該 ResourceDictionary通常不會有任何子項目。One 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指示詞的唯一值,當從 ResourceDictionary抓取值時,它會作為唯一索引鍵。Each 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.