MarkupExtension 类


为可以由 .NET Framework XAML 服务及其他 XAML 读取器和 XAML 编写器支持的 XAML 标记扩展实现提供基类。Provides a base class for XAML markup extension implementations that can be supported by .NET Framework XAML Services and other XAML readers and XAML writers.

public ref class MarkupExtension abstract
public abstract class MarkupExtension
type MarkupExtension = class
Public MustInherit Class MarkupExtension


标记扩展基于 XAML 中字符串属性值或标记元素的输入将对象返回给调用方。Markup extensions return objects to callers, based on the input of string attribute values or markup elements in XAML. 标记扩展以更复杂的方式返回对象,而不是单独的类型转换器。Markup extensions return objects in a more sophisticated way than type converters alone can accomplish. XAML 对象编写器调用类型转换器,因为类型或成员具有与之关联的类型转换器实现。A XAML object writer invokes a type converter because a type or member has a type converter implementation associated with it. 从引用的 CLR 框架,这意味着类型或成员 TypeConverterAttribute 特性化。From the CLR frame of reference, this means that a type or member is TypeConverterAttribute attributed. 从 XAML 类型系统角度来看,这意味着 XAML 类型或 XAML 成员具有其 TypeConverter 属性的值。From the XAML type system perspective, this means that a XAML type or a XAML member has a value for its TypeConverter property. 类型转换器的调用绑定到类型或属性定义,并始终在这些情况下由 XAML 处理调用。The invocation of a type converter is tied to the type or property definition, and is always invoked by XAML processing for these cases. 与此相反,标记扩展在控制用户代码和用户生成的标记时更多,可以在应用程序方案要求它时应用。In contrast, a markup extension is more under the control of user code and user-produced markup, and can be applied whenever an application scenario demands it. 可以调用标记扩展并将其用于设置不同的类型成员值,前提是标记扩展的返回类型可赋给该值。A markup extension can be invoked and can be used for setting different type-member values, so long as the markup extension's return type is assignable to that value.

有关创建自定义标记扩展的信息,请参阅 ProvideValueFor information on creating a custom markup extension, see ProvideValue. 有关常规标记扩展的详细信息,请参阅XAML 的标记扩展概述For more information on markup extensions in general, see Markup Extensions for XAML Overview. 如果是在 WPF 上生成并使用或创建 XAML 的标记扩展,则可以在主题标记扩展和 WPF XAML中找到其他相关信息。If you are building on WPF and using or creating markup extensions for XAML, other relevant information can be found in the topic Markup Extensions and WPF XAML.

每个标记扩展实现的 ProvideValue 方法可以在运行时使用 IServiceProvider 来提供上下文。The ProvideValue method of each markup extension implementation can use an IServiceProvider at run time that can provide context. 然后,针对传递信息的特定服务(如 IProvideValueTargetIXamlTypeResolver)查询此 IServiceProviderThis IServiceProvider is then queried for specific services that pass information, such as IProvideValueTarget or IXamlTypeResolver. 有关标记扩展的服务上下文的详细信息,请参阅可供类型转换器和标记扩展使用的服务上下文For more information on service contexts for a markup extension, see Service Contexts Available to Type Converters and Markup Extensions.

派生类应使用 MarkupExtensionReturnTypeAttribute 来向使用者通知标记扩展的 ProvideValue 实现中可用的最特定返回类型。Derived classes should be attributed with MarkupExtensionReturnTypeAttribute to inform consumers of the most specific return type available from the markup extension's ProvideValue implementation.



初始化从 MarkupExtension 派生的类的新实例。Initializes a new instance of a class derived from MarkupExtension.



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

(继承自 Object)

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

(继承自 Object)

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

(继承自 Object)

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

(继承自 Object)

当在派生类中实现时,返回用作此标记扩展的目标属性值的对象。When implemented in a derived class, returns an object that is provided as the value of the target property for this markup extension.


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

(继承自 Object)