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.


可以完全或部分可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML)定义的资源字典通常创建为属性元素, 并且通常位于任何单个页面或应用程序的根元素上。Resource dictionaries that can be defined completely or partially in 可扩展应用程序标记语言 (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. 可以设置ResourceDictionary属性, 以支持将元素的资源集合完全替换为新的或不同的方案。 ResourcesThe Resources property is settable to support the scenario of completely replacing the resources collection of an element to be a new or different ResourceDictionary.

请注意, 显示的ResourceDictionary语法不包含的元素。XAMLXAMLNotice 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" 指令, 该指令用作从中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.