ResourceDictionary 类

定义

提供一个哈希表/字典实现,其中包含组件所使用的 WPF 资源以及 WPF 应用程序的其他元素。Provides a hash table / dictionary implementation that contains WPF resources used by components and other elements of a WPF application.

public ref class ResourceDictionary : System::Collections::IDictionary, System::ComponentModel::ISupportInitialize, System::Windows::Markup::INameScope, System::Windows::Markup::IUriContext
[System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)]
[System.Windows.Markup.Ambient]
[System.Windows.Markup.UsableDuringInitialization(true)]
public class ResourceDictionary : System.Collections.IDictionary, System.ComponentModel.ISupportInitialize, System.Windows.Markup.INameScope, System.Windows.Markup.IUriContext
type ResourceDictionary = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
    interface ISupportInitialize
    interface IUriContext
    interface INameScope
Public Class ResourceDictionary
Implements IDictionary, INameScope, ISupportInitialize, IUriContext
继承
ResourceDictionary
派生
属性
实现

注解

XAMLXAML 加载程序加载应用程序代码时,不会立即处理 ResourceDictionary 中的项。The items in a ResourceDictionary are not immediately processed when application code is loaded by a XAMLXAML loader. 相反,ResourceDictionary 会保留为对象,并且仅当明确请求个别值时才处理这些值。Instead, the ResourceDictionary persists as an object, and the individual values are processed only when they are specifically requested.

ResourceDictionary 类不是从 DictionaryBase派生的。The ResourceDictionary class is not derived from DictionaryBase. 相反,ResourceDictionary 类实现 IDictionary,但依赖于内部 HashtableInstead, the ResourceDictionary class implements IDictionary but relies on a Hashtable internally.

可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML)中,ResourceDictionary 类通常是一个隐式集合元素,该元素是多个 Resources 属性的对象元素值(在属性元素语法中给定)。In 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax. 有关 XAMLXAML中的隐式集合的详细信息,请参阅XAML 语法详述For details on implicit collections in XAMLXAML, see XAML Syntax In Detail. 如果要指定合并字典,则会出现异常;有关详细信息,请参阅合并资源字典An exception is when you want to specify a merged dictionary; for details, see Merged Resource Dictionaries.

另一种可能的 XAMLXAML 用法是将资源字典声明为离散 XAMLXAML 文件,并在运行时将其加载到 Load,或将其作为资源或松散文件包含在(完全信任)项目中。Another possible XAMLXAML usage is to declare a resource dictionary as a discrete XAMLXAML file, and either load it at run time with Load or include it in a (full-trust) project as a resource or loose file. 在这种情况下,可以将 ResourceDictionary 声明为对象元素,作为 XAMLXAML的根元素。In this case, ResourceDictionary can be declared as an object element, serving as the root element of the XAMLXAML. 如果打算将其用作根元素,则必须将相应的 XML 命名空间值(WPFWPF 命名空间的默认值,通常 x: 用于 XAMLXAML 命名空间)映射到 ResourceDictionary 元素上。You must map the appropriate XML namespace values (default for the WPFWPF namespace and typically x: for the XAMLXAML namespace) onto the ResourceDictionary element if you plan to use it as the root element. 然后,可以添加定义资源的子元素,每个元素都有一个 " x:Key " 值。Then you can add child elements that define the resources, each with an x:Key value.

XAML 隐式集合用法XAML Implicit Collection Usage

<object>  
  <object.resourcesProperty>  
    oneOrMoreResources  
  </object.resourcesProperty>  
</object>  

XAML 值XAML Values

resourcesPropertyresourcesProperty
指定属性元素的属性,其中该属性的值类型为 ResourceDictionaryA property specifying a property element, where the value type of that property is ResourceDictionary. 通常,这是在应用程序资源的情况下 FrameworkElementFrameworkContentElementApplicationResources 属性。Typically, this is the Resources property of FrameworkElement or FrameworkContentElement, or of Application in an application resources case.

oneOrMoreResourcesoneOrMoreResources
指定为对象元素的一个或多个资源。One or more resources specified as object elements. 指定的每个资源都必须分配有 " x:Key" 指令Each resource that you specify must have an x:Key Directive assigned.

构造函数

ResourceDictionary()

初始化 ResourceDictionary 类的新实例。Initializes a new instance of the ResourceDictionary class.

属性

Count

获取基 ResourceDictionary 中的项数。Gets the number of entries in the base ResourceDictionary.

DeferrableContent

获取或设置此资源字典的可推迟内容。Gets or sets the deferrable content for this resource dictionary.

InvalidatesImplicitDataTemplateResources

获取或设置一个值,指示 ResourceDictionary 对象引发的失效是否会导致 ContentPresenter 对象重新评估其所选的模板。Gets or sets a value that indicates whether the invalidations fired by the ResourceDictionary object cause ContentPresenter objects to reevaluate their choice of template. 某个隐式数据模板资源发生改变时,将发生失效。The invalidations happen when an implicit data template resource changes.

IsFixedSize

获取此 ResourceDictionary 是否具有固定大小。Gets whether this ResourceDictionary is fixed-size.

IsReadOnly

获取此 ResourceDictionary 是否为只读。Gets whether this ResourceDictionary is read-only.

Item[Object]

获取或设置与给定键相关联的值。Gets or sets the value associated with the given key.

Keys

获取此 ResourceDictionary 中包含的所有键的集合。Gets a collection of all keys contained in this ResourceDictionary.

MergedDictionaries

获取 ResourceDictionary 字典的集合,这些字典构成了合并字典中的各种资源字典。Gets a collection of the ResourceDictionary dictionaries that constitute the various resource dictionaries in the merged dictionaries.

Source

获取或设置要从其加载资源的统一资源标识符 (URI)。Gets or sets the uniform resource identifier (URI) to load resources from.

Values

获取与此 ResourceDictionary 中包含的键关联的所有值的集合。Gets a collection of all values associated with keys contained in this ResourceDictionary.

方法

Add(Object, Object)

通过键将资源添加到此 ResourceDictionary 中。Adds a resource by key to this ResourceDictionary.

BeginInit()

开始此 ResourceDictionary 的初始化阶段。Begins the initialization phase for this ResourceDictionary.

Clear()

清除基 ResourceDictionary 中的所有键(以及值)。Clears all keys (and values) in the base ResourceDictionary. 这不会清除任何合并的字典项。This does not clear any merged dictionary items.

Contains(Object)

确定是否 ResourceDictionary 包含带有指定键的元素。Determines whether the ResourceDictionary contains an element with the specified key.

CopyTo(DictionaryEntry[], Int32)

ResourceDictionary 元素复制到位于指定索引处的一维 DictionaryEntry 中。Copies the ResourceDictionary elements to a one-dimensional DictionaryEntry at the specified index.

EndInit()

结束初始化阶段,并使上一个树无效,以便在初始化阶段对键所做的更改都可以得到解决。Ends the initialization phase, and invalidates the previous tree such that all changes made to keys during the initialization phase can be accounted for.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FindName(String)

此字典实现不支持该方法。Not supported by this Dictionary implementation.

GetEnumerator()

返回一个可用于循环访问 IDictionaryEnumeratorResourceDictionaryReturns an IDictionaryEnumerator that can be used to iterate through the ResourceDictionary.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnGettingValue(Object, Object, Boolean)

ResourceDictionary 接收资源请求时发生。Occurs when the ResourceDictionary receives a request for a resource.

RegisterName(String, Object)

此字典实现不支持该方法。Not supported by this Dictionary implementation.

Remove(Object)

从基字典中移除带有指定键的项。Removes the entry with the specified key from the base dictionary.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
UnregisterName(String)

此字典实现不支持该方法。Not supported by this Dictionary implementation.

显式界面实现

ICollection.CopyTo(Array, Int32)

有关此成员的说明,请参见 CopyTo(Array, Int32)For a description of this member, see CopyTo(Array, Int32).

ICollection.IsSynchronized

有关此成员的说明,请参见 IsSynchronizedFor a description of this member, see IsSynchronized.

ICollection.SyncRoot

有关此成员的说明,请参见 SyncRootFor a description of this member, see SyncRoot.

IEnumerable.GetEnumerator()

有关此成员的说明,请参见 GetEnumerator()For a description of this member, see GetEnumerator().

IUriContext.BaseUri

有关此成员的说明,请参见 BaseUriFor a description of this member, see BaseUri.

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅