浅谈C#控件属性串行化的实现

开发 后端
C#控件属性串行化的实现我们在这里主要向你介绍在C# WinForm控件开发中的应用,那么具体的实现属性是什么呢?通过介绍希望对你了解和学习C#控件属性串行化的实现有所帮助。

C#控件属性串行化是如何实现的呢?我们在这里向你介绍C# WinForm控件开发中的应用,那么具体的操作是什么呢?是用什么属性来实现呢?那么让我们来看看具体的实现。

C#控件属性串行化的相关概念介绍:

◆DesignerSerializationVisibilityAttribute的功能是指示一个属性是否串行化和如何串行化,它的值是一个枚举,一共有三种类型Content,Hidden,Visible。Content指示代码生成器为对象包含的内容生成代码,而不是为对象本身,Hidden指示代码生成器不为对象生成代码,visible指示代码生成器为对象生成代码。假如你的控件有一个集合属性,又想在设计时自动将集合属性的内容生成代码,那么就使用这个Attribute,并将值设为DesignerSerializationVisibility.Content。

◆TypeConverterAttribute的作用就更大一些,也稍微复杂一些。TypeConverterAttribute主要的目的是为属性指定一个类型转换器,这个转化器可以将属性的值转换城其它的类型。.NET框架已经为大部分常用的类型都提供了类型转换器,比如Color就有ColorConverter,枚举类型就有EnumConverter,等等,所以一般情况下你没有必要写类型转换器,如果你的属性的特殊的类型或者自定义的类型那么就必须要写了。类型转换器都是从System.ComponentModel.TypeConverter派生出来的,你需要重写其中的一些方法来达到转换的目的,在我们开发的过程中,其实只关心属性的值如何转换成字符串(因为属性的值需要在属性浏览器里显示出来,属性浏览器里显示的都是字符串)和源代码(需要自动为属性的值生成源代码以实现持久化),当然反过来,也要将字符串和源代码转换成属性的值。另外使用TypeConverter也可以实现子属性,让属性的子属性也显示在属性浏览器里,并且可以折叠。

C#控件属性串行化实例:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Windows.Forms;  
  5. using System.Drawing;  
  6. using System.ComponentModel;  
  7. using System.Collections;  
  8.  
  9. namespace CustomControlSample  
  10. {  
  11. public class MyListControl:System.Windows.Forms.Control  
  12. {  
  13. private List _list = new List();  
  14.  
  15. public MyListControl()  
  16. {  
  17.  
  18. }  
  19.  
  20. [Browsable(true)]  
  21. public List Item  
  22. {  
  23. get 
  24. {  
  25. return _list;  
  26. }  
  27. set 
  28. {  
  29. _list = value;  
  30. }  
  31. }  
  32.  
  33. protected override void OnPaint(PaintEventArgs e)  
  34. {  
  35. base.OnPaint(e);  
  36.  
  37. Graphics g = e.Graphics;  
  38. //绘制控件的边框  
  39.  
  40. g.DrawRectangle(Pens.Black,new Rectangle(Point.Empty,new Size(Size.Width-1,Size.Height-1)));  
  41.    
  42. for (Int32 i = 0; i < _list.Count; i++)  
  43. {  
  44. g.DrawString(_list[i].ToString(), Font, Brushes.Black,1, i * FontHeight);  
  45. }  
  46. }  
  47. }  
  48. }  

我创建了一个简单的List控件,将用户输入的数据显示在控件中,效果图如下:

简单的List控件 

在这个控件中,我声明了一个集合属性Item供用户输入要显示的整型数值。我们按照WinForm控件制作教程(二)中的方法将控件加到ToolBox里,然后拖到Form设计器中,然后选中控件,在属性浏览中查看控件的属性,属性中有一个Item的属性,属性右边的值显示为Collection,当你点击这个值的时候,值的右边出现一个小按钮,点击这个小按钮,就会出现弹出一个Collection Editor窗口,你可以在在这个编辑器里添加你想显示的整型值,如图:

Collection Editor窗口 

添加完以后,关闭Collection Editor。现在我们看看Form设计器为我们生成了什么代码。对于用户在Form设计器中设计的内容,设计器的代码生成器会将代码生成到窗口类的InitializeComponent()方法中,对于vs2005来说,这个方法位于***.Designer.cs文件中,在我当前的工程中位于Form1.Designer.cs文件中。在solution浏览器中双击打开这个文件,看看Form设计器为我们生成了什么代码:

  1.  //   
  2. // myListControl1  
  3. //   
  4. this.myListControl1.BackColor =   
  5. System.Drawing.SystemColors.ActiveCaptionText;  
  6. this.myListControl1.Item = ((  
  7. System.Collections.Generic.List<int>)  
  8. (resources.GetObject("myListControl1.Item")));  
  9. this.myListControl1.Location = new System.Drawing.Point(12, 34);  
  10. this.myListControl1.Name = "myListControl1";  
  11. this.myListControl1.Size = new System.Drawing.Size(220, 180);  
  12. this.myListControl1.TabIndex = 1;  
  13. this.myListControl1.Text = "myListControl1"

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

  1. [Browsable(true)]  
  2. [DesignerSerializationVisibilityAttribute(  
  3. DesignerSerializationVisibility.Content)]  
  4. public List Item  
  5. {  
  6. get 
  7. {  
  8. return _list;  
  9. }  
  10. set 
  11. {  
  12. _list = value;  
  13. }  

编辑完以后,Build控件工程,回到测试工程里,将Item属性里的值,删掉重新添加,添加完以后,我们再来看看设计器生成的代码:

  1.  //   
  2. // myListControl1  
  3. //   
  4. this.myListControl1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;  
  5. this.myListControl1.Item.Add(1);  
  6. this.myListControl1.Item.Add(2);  
  7. this.myListControl1.Item.Add(3);  
  8. this.myListControl1.Item.Add(6);  
  9. this.myListControl1.Item.Add(8);  
  10. this.myListControl1.Item.Add(9);  
  11. this.myListControl1.Location = new System.Drawing.Point(12, 34);  
  12. this.myListControl1.Name = "myListControl1";  
  13. this.myListControl1.Size = new System.Drawing.Size(220, 180);  
  14. this.myListControl1.TabIndex = 1;  
  15. this.myListControl1.Text = "myListControl1"

现在设计器将Item的内容串行化到源代码里了。

C#控件属性串行化的具体实现就向你介绍到这里,希望随你了解和C#控件属性串行化有所帮助。

【编辑推荐】

  1. 浅析C# WinForm控件开发前期准备
  2. 详解C# WinForm自定义控件的使用和调试
  3. C# Attribute的概念与使用浅析
  4. C# AttributeUsage的使用浅析
  5. 浅析Attribute在C# WinForm控件开发中的使用
责任编辑:仲衡 来源: IT168
相关推荐

2016-11-17 22:18:31

id串行化服务器

2009-07-10 09:38:06

Java swing组

2009-09-17 17:13:54

C#数组

2010-01-12 10:29:51

VB.NET对象串行化

2009-11-18 11:05:27

PHP串行化

2009-06-09 16:14:47

Java swing组件串行化

2009-11-02 16:41:55

VB.NET串行化对象

2009-08-11 10:12:21

2019-03-25 07:39:35

ID串行化消息顺序性高可用

2021-04-14 15:01:44

串行化方式缓存

2010-01-06 10:49:54

PHP串行化JSON

2009-08-13 15:40:29

C#数据绑定控件

2009-09-07 15:49:55

C#属性化的方法

2010-01-14 18:00:07

VB.NET串行化对象

2009-11-17 16:24:27

PHP变量串行化

2009-07-31 17:51:27

C#对象初始化

2009-08-07 13:03:10

C#控件数组

2009-06-16 10:20:05

多继承C#

2009-08-17 17:16:19

C#实现在线升级

2009-07-22 17:15:04

C#实现
点赞
收藏

51CTO技术栈公众号