淺談C#控件屬性串行化的實(shí)現(xiàn)
C#控件屬性串行化是如何實(shí)現(xiàn)的呢?我們?cè)谶@里向你介紹C# WinForm控件開發(fā)中的應(yīng)用,那么具體的操作是什么呢?是用什么屬性來實(shí)現(xiàn)呢?那么讓我們來看看具體的實(shí)現(xiàn)。
C#控件屬性串行化的相關(guān)概念介紹:
◆DesignerSerializationVisibilityAttribute的功能是指示一個(gè)屬性是否串行化和如何串行化,它的值是一個(gè)枚舉,一共有三種類型Content,Hidden,Visible。Content指示代碼生成器為對(duì)象包含的內(nèi)容生成代碼,而不是為對(duì)象本身,Hidden指示代碼生成器不為對(duì)象生成代碼,visible指示代碼生成器為對(duì)象生成代碼。假如你的控件有一個(gè)集合屬性,又想在設(shè)計(jì)時(shí)自動(dòng)將集合屬性的內(nèi)容生成代碼,那么就使用這個(gè)Attribute,并將值設(shè)為DesignerSerializationVisibility.Content。
◆TypeConverterAttribute的作用就更大一些,也稍微復(fù)雜一些。TypeConverterAttribute主要的目的是為屬性指定一個(gè)類型轉(zhuǎn)換器,這個(gè)轉(zhuǎn)化器可以將屬性的值轉(zhuǎn)換城其它的類型。.NET框架已經(jīng)為大部分常用的類型都提供了類型轉(zhuǎn)換器,比如Color就有ColorConverter,枚舉類型就有EnumConverter,等等,所以一般情況下你沒有必要寫類型轉(zhuǎn)換器,如果你的屬性的特殊的類型或者自定義的類型那么就必須要寫了。類型轉(zhuǎn)換器都是從System.ComponentModel.TypeConverter派生出來的,你需要重寫其中的一些方法來達(dá)到轉(zhuǎn)換的目的,在我們開發(fā)的過程中,其實(shí)只關(guān)心屬性的值如何轉(zhuǎn)換成字符串(因?yàn)閷傩缘闹敌枰趯傩詾g覽器里顯示出來,屬性瀏覽器里顯示的都是字符串)和源代碼(需要自動(dòng)為屬性的值生成源代碼以實(shí)現(xiàn)持久化),當(dāng)然反過來,也要將字符串和源代碼轉(zhuǎn)換成屬性的值。另外使用TypeConverter也可以實(shí)現(xiàn)子屬性,讓屬性的子屬性也顯示在屬性瀏覽器里,并且可以折疊。
C#控件屬性串行化實(shí)例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing;
- using System.ComponentModel;
- using System.Collections;
- namespace CustomControlSample
- {
- public class MyListControl:System.Windows.Forms.Control
- {
- private List
_list = new List(); - public MyListControl()
- {
- }
- [Browsable(true)]
- public List
Item - {
- get
- {
- return _list;
- }
- set
- {
- _list = value;
- }
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- Graphics g = e.Graphics;
- //繪制控件的邊框
- g.DrawRectangle(Pens.Black,new Rectangle(Point.Empty,new Size(Size.Width-1,Size.Height-1)));
- for (Int32 i = 0; i < _list.Count; i++)
- {
- g.DrawString(_list[i].ToString(), Font, Brushes.Black,1, i * FontHeight);
- }
- }
- }
- }
我創(chuàng)建了一個(gè)簡單的List控件,將用戶輸入的數(shù)據(jù)顯示在控件中,效果圖如下:
在這個(gè)控件中,我聲明了一個(gè)集合屬性Item供用戶輸入要顯示的整型數(shù)值。我們按照WinForm控件制作教程(二)中的方法將控件加到ToolBox里,然后拖到Form設(shè)計(jì)器中,然后選中控件,在屬性瀏覽中查看控件的屬性,屬性中有一個(gè)Item的屬性,屬性右邊的值顯示為Collection,當(dāng)你點(diǎn)擊這個(gè)值的時(shí)候,值的右邊出現(xiàn)一個(gè)小按鈕,點(diǎn)擊這個(gè)小按鈕,就會(huì)出現(xiàn)彈出一個(gè)Collection Editor窗口,你可以在在這個(gè)編輯器里添加你想顯示的整型值,如圖:
添加完以后,關(guān)閉Collection Editor?,F(xiàn)在我們看看Form設(shè)計(jì)器為我們生成了什么代碼。對(duì)于用戶在Form設(shè)計(jì)器中設(shè)計(jì)的內(nèi)容,設(shè)計(jì)器的代碼生成器會(huì)將代碼生成到窗口類的InitializeComponent()方法中,對(duì)于vs2005來說,這個(gè)方法位于***.Designer.cs文件中,在我當(dāng)前的工程中位于Form1.Designer.cs文件中。在solution瀏覽器中雙擊打開這個(gè)文件,看看Form設(shè)計(jì)器為我們生成了什么代碼:
- //
- // myListControl1
- //
- this.myListControl1.BackColor =
- System.Drawing.SystemColors.ActiveCaptionText;
- this.myListControl1.Item = ((
- System.Collections.Generic.List<int>)
- (resources.GetObject("myListControl1.Item")));
- this.myListControl1.Location = new System.Drawing.Point(12, 34);
- this.myListControl1.Name = "myListControl1";
- this.myListControl1.Size = new System.Drawing.Size(220, 180);
- this.myListControl1.TabIndex = 1;
- this.myListControl1.Text = "myListControl1";
設(shè)計(jì)器將Item的內(nèi)容串行化到了資源文件里?,F(xiàn)在我們修改控件的代碼,讓設(shè)計(jì)器將Item的內(nèi)容串行化到源代碼里。我們?yōu)镮tem屬性添加DesignerSerializationVisibilityAttribute,代碼片斷如下:
- [Browsable(true)]
- [DesignerSerializationVisibilityAttribute(
- DesignerSerializationVisibility.Content)]
- public List
Item - {
- get
- {
- return _list;
- }
- set
- {
- _list = value;
- }
- }
編輯完以后,Build控件工程,回到測試工程里,將Item屬性里的值,刪掉重新添加,添加完以后,我們?cè)賮砜纯丛O(shè)計(jì)器生成的代碼:
- //
- // myListControl1
- //
- this.myListControl1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
- this.myListControl1.Item.Add(1);
- this.myListControl1.Item.Add(2);
- this.myListControl1.Item.Add(3);
- this.myListControl1.Item.Add(6);
- this.myListControl1.Item.Add(8);
- this.myListControl1.Item.Add(9);
- this.myListControl1.Location = new System.Drawing.Point(12, 34);
- this.myListControl1.Name = "myListControl1";
- this.myListControl1.Size = new System.Drawing.Size(220, 180);
- this.myListControl1.TabIndex = 1;
- this.myListControl1.Text = "myListControl1";
現(xiàn)在設(shè)計(jì)器將Item的內(nèi)容串行化到源代碼里了。
C#控件屬性串行化的具體實(shí)現(xiàn)就向你介紹到這里,希望隨你了解和C#控件屬性串行化有所幫助。
【編輯推薦】