我正在使用组件,并且其中一个属性是type System.Type
。在大多数情况下,这都可以正常工作,但现在我发现自己需要int?
。这似乎不太简单,经过一番搜索和反复试验后,我得出了以下结论:
<local:NullableUInt32PropertyEditor PropertyType="{x:Type system:Nullable`1[System.UInt32]}"/>
这样可以编译并正常工作,但“错误”列表给出了
Nested types are not supported: Nullable`1[System.UInt32].
Intellisense还在该语句下给出了弯曲的线条,并且预览还指出了无效标记。
处理此问题的正确方法是什么?
谢谢杰夫
编辑:这与声明此问题中的值不同(使用XAML声明一个Nullable int(int?))。我需要声明类型,而不是值。
经过进一步研究后,我想出了以下解决方案:
public class NullableExtension : TypeExtension
{
public NullableExtension()
{
}
public NullableExtension(string type)
: base(type)
{
}
public NullableExtension(Type type)
: base(type)
{
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
Type basis = (Type)base.ProvideValue(serviceProvider);
return typeof(Nullable<>).MakeGenericType(basis);
}
}
然后在xaml中:
<local:NullableUInt32PropertyEditor PropertyType="{local:Nullable system:UInt32}"/>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句