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; }
public System.Windows.ResourceDictionary Resources { get; set; }
member this.Resources : System.Windows.ResourceDictionary with get, set
Public Property Resources As ResourceDictionary

Property Value


资源中当前本地定义的字典,其中的每个资源均可通过键进行访问。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. 可设置 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.

Applies to

See also