何时使用{X:类型...}? [英] when to use {x:Type …}?
问题描述
之间的区别是什么:
<Style TargetType="{x:Type Border}">
和
<Style TargetType="Border">
当和为什么我需要使用 {X:类型...}
?
When and why do I need to use the {x:Type …}
?
推荐答案
目前实际上是没有什么区别;在这两种情况下的TargetType属性将被设置为的typeof(境)
There is no difference in effect; in both cases the TargetType property will be set to typeof(Border)
第一个版本 {X:边框类型}
需要在WPF的第一个版本,因为编译器不使用类型转换器
类将字符串转换成一个类型的对象,你需要指定 TypeExtension
类来为你做的。
The first version {x:Type Border}
was needed in the first version of WPF because the compiler did not use the TypeConverter
class to convert the string into a Type object and you needed to specify the TypeExtension
class to do that for you.
第二个版本被引入,如果我没有记错,使用Silverlight并很快找到了自己的方式向WPF编译器。
The second version was introduced, if I remember correctly, with Silverlight and quickly found its way to the WPF compiler.
修改
我在类型转换器
类的假设是错误的;这是通过实施 FrameworkElementFactory
:
My assumption on the TypeConverter
class was wrong; this is implemented by the FrameworkElementFactory
:
从的文档:
这是支持的类型属性类型名,为字符串
WPF支持,使指定的一些价值技巧 类型标记扩展:类型类型,而不需要一个x属性 用法。相反,你可以指定值作为字符串名称 类型。这方面的例子是ControlTemplate.TargetType和 Style.TargetType。不会通过提供支持针对此行为 两种类型的转换器或标记扩展。相反,这是一个 通过FrameworkElementFactory实施延期的行为。
WPF supports techniques that enable specifying the value of some properties of type Type without requiring an x:Type markup extension usage. Instead, you can specify the value as a string that names the type. Examples of this are ControlTemplate.TargetType and Style.TargetType. Support for this behavior is not provided through either type converters or markup extensions. Instead, this is a deferral behavior implemented through FrameworkElementFactory.
Silverlight支持类似的约定。实际上,Silverlight那样 目前不支持{X:类型}在XAML语言支持,并执行 不接受{X:类型}用法的一些情况是外 旨在支持WPF,Silverlight的XAML迁移。因此,该 类型名-作为串行为是内置在所有的Silverlight天然 资产评估,其中一个类型的值。
Silverlight supports a similar convention. In fact, Silverlight does not currently support {x:Type} in its XAML language support, and does not accept {x:Type} usages outside of a few circumstances that are intended to support WPF-Silverlight XAML migration. Therefore, the typename-as-string behavior is built-in to all Silverlight native property evaluation where a Type is the value.
这篇关于何时使用{X:类型...}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!