IProvideValueTarget 接口

定义

表示一个服务,该服务报告标记扩展评估的情景对象和属性的关系。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

注解

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

当 xaml 对象编写器调用值转换IProvideValueTarget行为时, 类型转换器和标记扩展可以在 xaml 对象编写器提供的服务上下文上查询为服务。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.

IProvideValueTarget尽管 API 名称可能表示与IProvideValueTarget MarkupExtension.ProvideValue关联, 但通过报告的信息对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.

尝试使用服务之前, 请确保服务本身不null是由用于上下文的相关服务提供程序参数返回。Before 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.

重要

使用该服务时, 不要修改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.

备注

不要使用类型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 4IProvideValueTarget , 为 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.

IProvideValueTarget在的内部实现BindingBase(即在 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. 一个示例案例, 其中的值IProvideValueTarget即使在服务可用时也为 null, 这是Binding因为标记扩展嵌套在其他标记扩展中, 例如。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.

属性

TargetObject

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

TargetProperty

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

适用于

另请参阅