IProvideValueTarget Interface

Definition

表示一个服务,该服务报告标记扩展评估的情景对象和属性的关系。Represents a service that reports situational object-property relationships for markup extension evaluation.

public interface class IProvideValueTarget
public interface IProvideValueTarget
type IProvideValueTarget = interface
Public Interface IProvideValueTarget

Remarks

IProvideValueTarget 表示一项服务,该服务报告有关在其中调用值转换器的对象-属性结构的信息。IProvideValueTarget represents a service that reports information regarding the object-property structure surrounding where a value converter is invoked.

当 XAML 对象编写器调用值转换行为时,类型转换器和标记扩展可以在 XAML 对象编写器提供的服务上下文上查询 IProvideValueTarget 作为服务。Type converters and markup extensions can query for IProvideValueTarget as a service on the service context that is provided by a XAML object writer, when the XAML object writer invokes value conversion behavior. 有关详细信息,请参阅可用于类型转换器和标记扩展的 XAML 的类型转换器和标记扩展For more information, see Type Converters and Markup Extensions for XAML and Service Contexts Available to Type Converters and Markup Extensions.

特别是对于标记扩展,IProvideValueTarget 很有用的情况是确定标记扩展的具体使用情况的有效性。For markup extensions specifically, a scenario where IProvideValueTarget is useful is to determine the validity of your markup extension's situational usage. 例如,标记扩展可以实现值延迟机制,但使用该机制可能仅适用于在运行时目标对象能够使用延迟机制的其他组件的情况。For example, your markup extension might implement a value deferral mechanism, but using that mechanism might only be appropriate if the target object is capable of using other components of your deferral mechanism at run time. 您可以检查作为 TargetObject 返回的对象的类型系统特征,并确保对象支持您的延迟技术。You could check the type system characteristics of the object returned as TargetObject and make sure that object supports your deferral technique. 如果是这样,则标记扩展将返回一个值,如果不是,则标记扩展将引发一个异常,该异常将被 XAML 对象编写器作为内部异常选取。If so, your markup extension returns a value, and if not, your markup extension throws an exception that will be picked up as an inner exception by a XAML object writer. 类似的方案是通过检查 TargetProperty的类型系统特征来确保特定属性支持功能。A similar scenario is assuring that a particular property supports a capability, by checking type system characteristics of the TargetProperty.

尽管 API 名称 IProvideValueTarget 可能表示与 MarkupExtension.ProvideValue关联,但 IProvideValueTarget 报告的信息也对 TypeConverterValueSerializer很有用。Although the API name IProvideValueTarget might imply an association with MarkupExtension.ProvideValue, the information reported by IProvideValueTarget can also be useful to a TypeConverter or ValueSerializer. TypeConverterValueSerializer 通常具有比标记扩展更多的上下文,因为它们专用于特定目标类型或目标属性,因此,通常情况下,在类型系统中可预测具体的有效性。A TypeConverter or ValueSerializer typically have more context than a markup extension because they are dedicated to a particular target type or target property, and thus the situational validity is usually predictable in the type system. 但存在其他方案。However, other scenarios exist. 下面列出了可能由类型转换器或标记扩展实现的某些方案:The following is a list of some scenarios that might be implemented by either a type converter or a markup extension:

  • 获取 TargetObject 读取该对象上其他属性的实例值。Get TargetObject to read instance values of other properties on that object. 请注意,由于目标对象上的 XAML 成员的处理顺序可能是不确定的,因此在此处处理可能需要为不设置的值提供强大的功能。Note that your handling here might need to be robust for unset values, because the processing order of XAML members on the target object can be nondeterministic.

  • 获取 TargetObject 读取类型级元数据,或验证所需的接口是否由该类型实现。Get TargetObject to read type-level metadata, or to verify that required interfaces are implemented by the type.

  • 获取 TargetProperty 读取属性元数据。Get TargetProperty to read property metadata.

一种方案是使用 IProvideValueTarget 信息来限制允许标记扩展设置的对象或属性。One scenario is to use IProvideValueTarget information to limit which objects or properties are permitted to be set by your markup extension.

尝试使用服务之前,请确保在用于上下文的相关服务提供程序参数返回后,服务本身不 nullBefore attempting to use the service, make sure that the service itself is not null when returned by the relevant service provider parameter you are using for context.

Important

使用该服务时,不修改由 TargetObject 属性引用的对象或该对象引用的其他对象,这一点特别重要。When using the service, it is particularly important not to modify the object referenced by the TargetObject property, or other objects referenced by that object. 特别是对于 WPF,这些更改可能会导致意外的更改,从而使 WPF 属性系统无效。For WPF specifically, these changes could cause unexpected changes that would invalidate the WPF property system.

Note

不要使用 TargetProperty 类型来确定标记扩展或类型转换器应返回的精确类型。Do not use the type of TargetProperty to determine which precise type a markup extension or type converter should return. 这不是最佳做法。That is not a best practice. 返回类型应与特性(标记扩展的MarkupExtensionReturnTypeAttribute)或模式(类型转换器的类名称)一致。Your return type should be consistent with attributing (MarkupExtensionReturnTypeAttribute for markup extensions) or patterns (class name of the type converter).

在 .NET Framework 的以前版本中,此接口存在于 WPF 特定的程序集 WindowsBase 中。In previous versions of the .NET Framework, this interface existed in the WPF-specific assembly WindowsBase. .NET Framework 4.NET Framework 4中,IProvideValueTarget 在 system.exception 程序集中。In .NET Framework 4.NET Framework 4, IProvideValueTarget is in the System.Xaml assembly. 有关详细信息,请参阅 Types Migrated from WPF to System.XamlFor more information, see Types Migrated from WPF to System.Xaml.

WPF 用法说明WPF Usage Notes

此服务支持框架功能,如依赖属性上的绑定。This service supports framework features such as binding on a dependency property.

在 WPF 中,当从标记扩展或类型转换器返回的值取决于上下文时,由 IProvideValueTarget 表示的服务可以返回对象和属性。In WPF, the service represented by IProvideValueTarget can return objects and properties for situations where the value to return from a markup extension or type converter depends on context.

IProvideValueTargetBindingBase的内部实现中广泛利用,该类在 WPF 中实现依赖项属性的绑定机制。IProvideValueTarget is leveraged heavily in the internal implementation of BindingBase, the class that implements the binding mechanism for dependency properties in WPF.

WPF 的默认内部服务提供程序通过自定义标记扩展或类型转换器实现 IProvideValueTarget 以供使用。A default internal service provider for WPF implements IProvideValueTarget for usage by a custom markup extension or type converter. IProvideValueTarget 报告的对象和属性只能由内部 WPF 服务设置。The object and property reported by IProvideValueTarget can only be set by the internal WPF service. 超出 IProvideValueTarget 操作方式的定义,需要为标记扩展和类型转换器用法定义你自己的服务提供程序,该提供程序至少以 IProvideValueTargetIXamlTypeResolver 服务实现,这是未在此处介绍的高级方案。Going beyond this definition of how IProvideValueTarget operates would require defining your own service provider for markup extension and type converter usages that implements at the very least the IProvideValueTarget and IXamlTypeResolver services, and that is an advanced scenario that is not described here.

TargetPropertyTargetObject 的 null 值并不一定意味着标记扩展或类型转换器不能操作,尽管这取决于实现。A null value for TargetProperty or TargetObject does not necessarily mean that a markup extension or type converter cannot act, although that depends on your implementation. 例如,如果你的标记扩展嵌套在另一个标记扩展中(如 Binding),则 IProvideValueTarget 中的值为 null。An example case where the values from IProvideValueTarget are null even if the service is available is if your markup extension is nested within another markup extension, such as a Binding. 依赖于 IProvideValueTarget 的实现应始终检查 null 值(如果仅出于引发特定异常的目的)。An implementation that relies on IProvideValueTarget should always check for null values if only for the purpose of throwing a specific exception, if that is your intended design.

Properties

TargetObject

获取被报告的目标对象。Gets the target object being reported.

TargetProperty

获取正在报告的目标属性的标识符。Gets an identifier for the target property being reported.

Applies to

See also