自己创建了个类用于收纳控件属性,这个类带有个Image类型的字段,但在使用的时候,字符串等类型数据都能成功记录设计时设置,Image就不可以,该如何解决?代码如下:
/// <summary>
/// Banner内容信息类
/// </summary>
[TypeConverter(typeof(BannerContentInfoConverter))]
public sealed class BannerContentInfo
{
private string _caption = "标题";
/// <summary>
/// 获取或设置Banner标题
/// </summary>
[Category("Appearance"), DefaultValue("标题"), Description("设置Banner标题")]
public string Caption
{
get
{
return _caption;
}
set
{
if (value == null)
{
value = "";
}
if (value != this._caption)
{
this._caption = value;
this.EnsureInvalidate(false);
}
}
} private string _description = "";
/// <summary>
/// 获取或设置Banner说明
/// </summary>
[Category("Appearance"), DefaultValue(""), Description("设置Banner说明")]
public string Description
{
get
{
return _description;
}
set
{
if (value == null)
{
value = "";
}
if (value != this._description)
{
_description = value;
this.EnsureInvalidate(false);
}
}
} private Image _glyph = null;
/// <summary>
/// 获取或设置Banner图标
/// </summary>
[Category("Appearance"), DefaultValue((string)null), Description("设置Banner图标")]
public Image Glyph
{
get
{
return this._glyph;
}
set
{
if (_glyph != null)
{
this._glyph = value;
this.EnsureInvalidate(false);
}
}
} /// <summary>
/// 获取Banner内容的文字形式
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "[{0}: Caption={1}, Description={2}, Units={3}, GdiCharSet={4}, GdiVerticalFont={5}]", new object[] { base.GetType().Name, this.Caption, this.Description});//, (int)this.fontUnit, this.gdiCharSet, this.gdiVerticalFont });
}
}
/// <summary>
/// Banner内容信息类
/// </summary>
[TypeConverter(typeof(BannerContentInfoConverter))]
public sealed class BannerContentInfo
{
private string _caption = "标题";
/// <summary>
/// 获取或设置Banner标题
/// </summary>
[Category("Appearance"), DefaultValue("标题"), Description("设置Banner标题")]
public string Caption
{
get
{
return _caption;
}
set
{
if (value == null)
{
value = "";
}
if (value != this._caption)
{
this._caption = value;
this.EnsureInvalidate(false);
}
}
} private string _description = "";
/// <summary>
/// 获取或设置Banner说明
/// </summary>
[Category("Appearance"), DefaultValue(""), Description("设置Banner说明")]
public string Description
{
get
{
return _description;
}
set
{
if (value == null)
{
value = "";
}
if (value != this._description)
{
_description = value;
this.EnsureInvalidate(false);
}
}
} private Image _glyph = null;
/// <summary>
/// 获取或设置Banner图标
/// </summary>
[Category("Appearance"), DefaultValue((string)null), Description("设置Banner图标")]
public Image Glyph
{
get
{
return this._glyph;
}
set
{
if (_glyph != null)
{
this._glyph = value;
this.EnsureInvalidate(false);
}
}
} /// <summary>
/// 获取Banner内容的文字形式
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "[{0}: Caption={1}, Description={2}, Units={3}, GdiCharSet={4}, GdiVerticalFont={5}]", new object[] { base.GetType().Name, this.Caption, this.Description});//, (int)this.fontUnit, this.gdiCharSet, this.gdiVerticalFont });
}
}
解决方案 »
- C#使用连接池连接Mysql数据库
- C#用system.io.directory.getfiles查找文件出错的问题
- 怎么让用VS 生成 __doPostBack('','') 函数啊????
- 关于VS和XNA
- 各位高手快来.高分求Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient) 实现断点续传的新讨论,附源码
- 对于加密文件的使用问题
- 未能在全局命名空间中找到类型或命名空间名称"cuteeditor"(是否缺少程序集引用)?
- 在asp.net中,如何把自己的网页转换为图片,asp.net是用不了Webbrowser的
- 未处理的“System.NullReferenceException”类型的异常出现在 system.data.dll 中。
- 水晶报表,想说爱你不容易,在线求助!!
- c# scoket
- 我的vs2008安装后为什么没有System.Windows.Media和Windows Presentation Foundation呢
{
private const string DataHdr = "data="; public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
} public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
} public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string text = (value as string).Trim();
if (text.Length == 0)
{
return new BannerContentInfo();
}
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
string separator = culture.TextInfo.ListSeparator + " " + DataHdr;
string[] datas = text.Split(new string[] { separator }, StringSplitOptions.None);
if (datas.Length < 2 || datas.Length > 3)
{
throw new ArgumentException("1|" + separator + "|" + value.ToString());
}
string caption = datas[0];
string description = datas[1];
Image glyph = null;
if(datas.Length == 3)
{
try
{
byte[] glyphData = Convert.FromBase64String(datas[2]);
TypeConverter convert = TypeDescriptor.GetConverter(typeof(Image));
if (convert != null)
{
glyph = convert.ConvertFrom(glyphData) as Image;
}
}
catch
{
}
}
return new BannerContentInfo() { Caption = caption, Description = description, Glyph = glyph };
}
if (value is BannerContentInfo)
{
BannerContentInfo content = value as BannerContentInfo;
return new BannerContentInfo() { Caption = content.Caption, Description = content.Description, Glyph = content.Glyph };
}
return base.ConvertFrom(context, culture, value);
} public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (destinationType == typeof(string))
{
BannerContentInfo content = value as BannerContentInfo;
if (content == null)
{
return "toStringNone";
}
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
string separator = culture.TextInfo.ListSeparator + " " + DataHdr;
int count = 2;
if (content.Glyph != null)
{
count++;
}
string[] datas = new string[count];
int index = 0;
datas[index++] = content.Caption;
datas[index++] = content.Description;
if (content.Glyph != null)
{
TypeConverter convert = TypeDescriptor.GetConverter(typeof(Image));
byte[] glyph = convert.ConvertTo(content.Glyph, typeof(byte[])) as byte[];
datas[index++] = Convert.ToBase64String(glyph);
}
return string.Join(separator, datas);
}
if ((destinationType == typeof(InstanceDescriptor)) && (value is BannerContentInfo))
{
BannerContentInfo content = (BannerContentInfo)value;
return new BannerContentInfo() { Caption = content.Caption, Description = content.Description, Glyph = content.Glyph };
}
return base.ConvertTo(context, culture, value, destinationType);
} public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
string caption = propertyValues["Caption"] as string;
string description = propertyValues["Description"] as string;
Image glyph = propertyValues["Glyph"] as Image;
return new BannerContentInfo() { Caption = caption, Description = description, Glyph = glyph };
} public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
} public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(BannerContentInfo), attributes).Sort(new string[] { "Caption", "Description", "Glyph" });
} public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}
public Image Glyph
{
get
{
return this._glyph;
}
set
{
//if (_glyph != null)
if( value != null) //<------------
{
this._glyph = value;
this.EnsureInvalidate(false);
}
}
}
/// <summary>
/// 获取或设置Banner图标
/// </summary>
[Category("Appearance"), DefaultValue((string)null), Description("设置Banner图标")]
public Image Glyph
{
get
{
return this._glyph;
}
set
{
if (_glyph != null)
{
this._glyph = value;
this.EnsureInvalidate(false);
}
}
}
你TypeConverter中转换为InstanceDescriptor的实现有误
class My : Component
{
BannerContentInfo bi = new BannerContentInfo();
public BannerContentInfo BB { get { return bi; } set { bi = value; } }
} [TypeConverter(typeof(ExpandableObjectConverter))] //<---
public sealed class BannerContentInfo
{
//...
}