Silverlight XAML是否支持字节数据类型? [英] Does Silverlight XAML Support The Byte Data Type?

查看:89
本文介绍了Silverlight XAML是否支持字节数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据类型:

using System;

namespace UI 
{
    public class AddressType
    {
        public byte ID { get; set; }
        public string Name { get; set; }
    } 
}

这是我的收藏集:

using System.Collections.ObjectModel;

namespace UI
{
    public class AddressTypes : ObservableCollection<AddressType>
    {
    }
}


Here is my XAML from my UserControl.Resources section of my page:

< local:AddressTypes x:Name ="AddressTypesList">

<local:AddressTypes x:Name="AddressTypesList">

    <local:AddressType ID="0" Name="Select"/>
    <local:AddressType ID="1" Name="Office"/>
    <local:AddressType ID="2" Name="Shipping"/>
    <local:AddressType ID="3" Name="Warehouse"/>
    <local:AddressType ID="4" Name="Home"/>
    <local:AddressType ID="5" Name="Foreign"/>

</local:AddressTypes>

当我尝试将XAML中的值分配给ID属性时,出现AG_E_PARSER_BAD_PROPERTY_VALUE [行:10位置:35]错误.如果将ID属性的数据类型更改为int,一切都很好. Silverlight不支持字节数据类型吗?

When I try to assign a value in XAML to the ID property, I get a AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 10 Position: 35] error. If I change the data type of the ID property to int, all is well. Doesn't Silverlight support the byte data type?

推荐答案

使用属性语法指定字节值似乎不起作用.但是,可以使用属性元素语法指定字节值. 添加以下xmlns声明:

Specifying byte values using the attribute syntax does not appear to work. However, it is possible to specify byte values using the property element syntax. Add the the following xmlns declaration:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

您应该能够像这样指定字节属性:

The you should be able to specify the byte properties like so:

<local:AddressType Name="Select">
  <local:AddressType.ID>
    <sys:Byte>0</sys:Byte>
  </local:AddressType.ID>
</local:AddressType>

这有点混乱,因此您可以实现自定义类型转换器,并使用属性标记您的属性以使用该类型转换器.

This is kind of messy though, so what you can do is implement a custom type converter, and mark up your property with an attribute to use that type converter.

TypeConverter应该类似于:

The TypeConverter should look something like:

public class ByteTypeConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value is string)
        {
            return Byte.Parse(value as string);
        }
        else
        {
            return base.ConvertFrom(context, culture, value);
        }
    }
}

然后您要修改您的类,以便该属性指向此类型转换器:

And you then want to modify your class so that the property points to this type converter:

public class AddressType
{
    [TypeConverter(typeof(ByteTypeConverter))]
    public byte ID { get; set; }
    public string Name { get; set; }
}

现在,您应该可以使用常规属性的语法:

Now you should be able to use the regular property attribute syntax:

<local:AddressType ID="0" Name="Select"/>

这篇关于Silverlight XAML是否支持字节数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆