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
[System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)]
public 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)>]
type ResourceDictionary = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
    interface ISupportInitialize
    interface INameScope
    interface IUriContext
[<System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)>]
type ResourceDictionary = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
    interface INameScope
    interface ISupportInitialize
    interface IUriContext
[<System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)>]
[<System.Windows.Markup.Ambient>]
[<System.Windows.Markup.UsableDuringInitialization(true)>]
type ResourceDictionary = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
    interface ISupportInitialize
    interface IUriContext
    interface INameScope
Public Class ResourceDictionary
Implements IDictionary, INameScope, ISupportInitialize, IUriContext
继承
ResourceDictionary
派生
属性
实现

注解

ResourceDictionaryXAML 加载程序加载应用程序代码时,不会立即处理中的项。The items in a ResourceDictionary are not immediately processed when application code is loaded by a XAML loader. 相反,将 ResourceDictionary 以对象的形式保留,单独的值仅在被明确请求时处理。Instead, the ResourceDictionary persists as an object, and the individual values are processed only when they are specifically requested.

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

在 Extensible Application Markup Language (XAML) , ResourceDictionary 类通常是一个隐式集合元素,该元素是多个属性的对象元素值 Resources (在属性元素语法中给定)。In 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. 有关 XAML 中的隐式集合的详细信息,请参阅 Xaml 语法详述For details on implicit collections in XAML, see XAML Syntax In Detail. 如果要指定合并字典,则会出现异常;有关详细信息,请参阅 合并资源字典An exception is when you want to specify a merged dictionary; for details, see Merged Resource Dictionaries.

另一种可能的 XAML 用法是将资源字典声明为离散 XAML 文件,并在运行时将其加载到 Load 中,或将其作为资源或松散文件包含在 (完全信任的) 项目中。Another possible XAML usage is to declare a resource dictionary as a discrete XAML 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 可以将声明为对象元素,作为 XAML 的根元素。In this case, ResourceDictionary can be declared as an object element, serving as the root element of the XAML. 如果打算将其用作根元素,则必须将相应的 XML 命名空间值映射 (WPF 命名空间的默认值,并且通常 x: 用于 XAML 命名空间) 到 ResourceDictionary 元素。You must map the appropriate XML namespace values (default for the WPF namespace and typically x: for the XAML 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. 通常,这是 Resources FrameworkElementFrameworkContentElement 的属性,或 Application 在应用程序资源的情况下。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.

适用于

另请参阅