开发者

Silverlight 3 custom control: only 'int' as numeric type for a property? [e.g. long or int64 seems to break]

I'm trying to add a new property to a Silverlight 3 custom control of mine. An int property works perfectly, but if I change it to a long or an int64, I've got a xaml parser exception on run开发者_高级运维time.

Do you know if it's a known restriction in SL3?

C# side, new control:

public class myExtTextBox : TextBox
{
    public int MaxNumericValue { get; set; }
    //public long MaxLongNumericValue { get; set; }  => This breaks the parser
}

XAML side:

<myExtTextBox x:Name="foobar"  MaxNumericValue="12" /> <!-- OK -->

<myExtTextBox x:Name="foobar"  MaxLongNumericValue="12" /> <!-- Breaks parser -->


It is odd in my view that Xaml can't parse all types that implement IConvertible. (I'd love to know the reason if any of you MS lurkers care to edify?)

Here is an implementation that may help:-

public class ConvertibleTypeConverter<T> : TypeConverter where T: IConvertible
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType.GetInterface("IConvertible", false) != null;
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType.GetInterface("IConvertible", false) != null;
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        return ((IConvertible)value).ToType(typeof(T), culture);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        return ((IConvertible)value).ToType(destinationType, culture);
    }
}

Now on the MaxLongNumericValue property you use an attribute like this:-

[TypeConverter(typeof(ConvertibleTypeConverter<long>))]
public long MaxLongNumericValue { get; set; }

Now when the Xaml parser gets to this property it will defer to the specified TypeConverter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜