WinForm控件开发总结(四)-----控件属性的串行化

        前一篇文章介绍了常用的设计时 Attribute 。其中 BrowsableAttribute , CategoryAttribute , DescriptionAttribute , DefaultPropertyAttribute , DefaultEventAttribute 都是比较简单的,也是可有可无,但是为了提供更好的用户体验这些 Attribute 最好不要省掉,如果你对这些Attribute还不熟悉,可以参考我前一篇文章的描述或者查看MSDN,这里我就不在赘述了。
       下来我们主要介绍一下 DesignerSerializationVisibilityAttribute 和 TypeConverterAttribute 。
       DesignerSerializationVisibilityAttribute 的功能是指示一个属性是否串行化和如何串行化,它的值是一个枚举,一共有三种类型 Content , Hidden , Visible 。 Content 指示代码生成器为对象包含的内容生成代码,而不是为对象本身, Hidden 指示代码生成器不为对象生成代码, visible 指示代码生成器为对象生成代码。假如你的控件有一个集合属性,又想在设计时自动将集合属性的内容生成代码,那么就使用这个 Attribute ,并将值设为 DesignerSerializationVisibility.Content 。
      TypeConverterAttribute 的作用就更大一些,也稍微复杂一些。 TypeConverterAttribute 主要的目的是为属性指定一个类型转换器,这个转化器可以将属性的值转换城其它的类型。 .NET 框架已经为大部分常用的类型都提供了类型转换器,比如 Color 就有 ColorConverter ,枚举类型就有 EnumConverter ,等等,所以一般情况下你没有必要写类型转换器,如果你的属性的特殊的类型或者自定义的类型那么就必须要写了。类型转换器都是从 System.ComponentModel.TypeConverter 派生出来的,你需要重写其中的一些方法来达到转换的目的,在我们开发的过程中,其实只关心属性的值如何转换成字符串(因为属性的值需要在属性浏览器里显示出来,属性浏览器里显示的都是字符串)和源代码(需要自动为属性的值生成源代码以实现持久化),当然反过来,也要将字符串和源代码转换成属性的值。另外使用 TypeConverter 也可以实现子属性,让属性的子属性也显示在属性浏览器里,并且可以折叠。
       接下来我就写一个简单的控件来演示一下这个控件。代码如下:
      
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Drawing;
None.gif
using  System.ComponentModel;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  CustomControlSample
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class MyListControl:System.Windows.Forms.Control
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private List<Int32> _list = new List<Int32>();
InBlock.gif
InBlock.gif        
public MyListControl()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Browsable(
true)]
InBlock.gif        
public List<Int32> Item
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _list;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _list 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnPaint(PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnPaint(e);
InBlock.gif
InBlock.gif            Graphics g 
= e.Graphics;
InBlock.gif            
//绘制控件的边框
InBlock.gif

InBlock.gif            g.DrawRectangle(Pens.Black,
new Rectangle(Point.Empty,new Size(Size.Width-1,Size.Height-1)));
InBlock.gif   
InBlock.gif            
for (Int32 i = 0; i < _list.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                g.DrawString(_list[i].ToString(), Font, Brushes.Black,
1, i * FontHeight);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

       我创建了一个简单的 List 控件,将用户输入的数据显示在控件中,效果图如下:
   
     在这个控件中,我声明了一个集合属性Item供用户输入要显示的整型数值。我们按照WinForm控件制作教程(二)中的方法将控件加到ToolBox里,然后拖到Form设计器中,然后选中控件,在属性浏览中查看控件的属性,属性中有一个Item的属性,属性右边的值显示为Collection,当你点击这个值的时候,值的右边出现一个小按钮,点击这个小按钮,就会出现弹出一个Collection Editor窗口,你可以在在这个编辑器里添加你想显示的整型值,如图:
      
     添加完以后,关闭Collection Editor。现在我们看看Form设计器为我们生成了什么代码。对于用户在Form设计器中设计的内容,设计器的代码生成器会将代码生成到窗口类的InitializeComponent()方法中,对于vs2005来说,这个方法位于***.Designer.cs文件中,在我当前的工程中位于Form1.Designer.cs文件中。在solution浏览器中双击打开这个文件,看看Form设计器为我们生成了什么代码:
      
None.gif              //  
None.gif            
//  myListControl1
None.gif            
//  
None.gif
             this .myListControl1.BackColor  =  System.Drawing.SystemColors.ActiveCaptionText;
None.gif            
this .myListControl1.Item  =  ((System.Collections.Generic.List < int > )(resources.GetObject( " myListControl1.Item " )));
None.gif            
this .myListControl1.Location  =   new  System.Drawing.Point( 12 34 );
None.gif            
this .myListControl1.Name  =   " myListControl1 " ;
None.gif            
this .myListControl1.Size  =   new  System.Drawing.Size( 220 180 );
None.gif            
this .myListControl1.TabIndex  =   1 ;
None.gif            
this .myListControl1.Text  =   " myListControl1 " ;
None.gif
      

      设计器将Item的内容串行化到了资源文件里。现在我们修改控件的代码,让设计器将Item的内容串行化到源代码里。我们为Item属性添加DesignerSerializationVisibilityAttribute,代码片断如下:

      
None.gif [Browsable( true )]
None.gif        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
None.gif        
public  List < Int32 >  Item
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _list;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _list 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
       编辑完以后, Build 控件工程,回到测试工程里,将 Item 属性里的值,删掉重新添加,添加完以后,我们再来看看设计器生成的代码:
      
None.gif //  
None.gif            
//  myListControl1
None.gif            
//  
None.gif
             this .myListControl1.BackColor  =  System.Drawing.SystemColors.ActiveCaptionText;
None.gif            
this .myListControl1.Item.Add( 1 );
None.gif            
this .myListControl1.Item.Add( 2 );
None.gif            
this .myListControl1.Item.Add( 3 );
None.gif            
this .myListControl1.Item.Add( 6 );
None.gif            
this .myListControl1.Item.Add( 8 );
None.gif            
this .myListControl1.Item.Add( 9 );
None.gif            
this .myListControl1.Location  =   new  System.Drawing.Point( 12 34 );
None.gif            
this .myListControl1.Name  =   " myListControl1 " ;
None.gif            
this .myListControl1.Size  =   new  System.Drawing.Size( 220 180 );
None.gif            
this .myListControl1.TabIndex  =   1 ;
None.gif            
this .myListControl1.Text  =   " myListControl1 " ;
None.gif
       现在设计器将 Item 的内容串行化到源代码里了。
      时间有限,今天就写到这里,下一篇文章我来介绍 TypeConverterAttribute。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值