自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談C#控件屬性串行化的實(shí)現(xiàn)

開發(fā) 后端
C#控件屬性串行化的實(shí)現(xiàn)我們?cè)谶@里主要向你介紹在C# WinForm控件開發(fā)中的應(yīng)用,那么具體的實(shí)現(xiàn)屬性是什么呢?通過介紹希望對(duì)你了解和學(xué)習(xí)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í)例:

  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. }  

我創(chuàng)建了一個(gè)簡單的List控件,將用戶輸入的數(shù)據(jù)顯示在控件中,效果圖如下:

簡單的List控件 

在這個(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è)編輯器里添加你想顯示的整型值,如圖:

Collection Editor窗口 

添加完以后,關(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ì)器為我們生成了什么代碼:

  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"

設(shè)計(jì)器將Item的內(nèi)容串行化到了資源文件里?,F(xiàn)在我們修改控件的代碼,讓設(shè)計(jì)器將Item的內(nèi)容串行化到源代碼里。我們?yōu)镮tem屬性添加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屬性里的值,刪掉重新添加,添加完以后,我們?cè)賮砜纯丛O(shè)計(jì)器生成的代碼:

  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"

現(xiàn)在設(shè)計(jì)器將Item的內(nèi)容串行化到源代碼里了。

C#控件屬性串行化的具體實(shí)現(xiàn)就向你介紹到這里,希望隨你了解和C#控件屬性串行化有所幫助。

【編輯推薦】

  1. 淺析C# WinForm控件開發(fā)前期準(zhǔn)備
  2. 詳解C# WinForm自定義控件的使用和調(diào)試
  3. C# Attribute的概念與使用淺析
  4. C# AttributeUsage的使用淺析
  5. 淺析Attribute在C# WinForm控件開發(fā)中的使用
責(zé)任編輯:仲衡 來源: IT168
相關(guān)推薦

2016-11-17 22:18:31

id串行化服務(wù)器

2009-09-17 17:13:54

C#數(shù)組

2009-07-10 09:38:06

Java swing組

2010-01-12 10:29:51

VB.NET對(duì)象串行化

2009-11-18 11:05:27

PHP串行化

2009-06-09 16:14:47

Java swing組件串行化

2009-08-11 10:12:21

2009-11-02 16:41:55

VB.NET串行化對(duì)象

2019-03-25 07:39:35

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

2009-08-13 15:40:29

C#數(shù)據(jù)綁定控件

2009-09-07 15:49:55

C#屬性化的方法

2010-01-06 10:49:54

PHP串行化JSON

2021-04-14 15:01:44

串行化方式緩存

2009-07-31 17:51:27

C#對(duì)象初始化

2009-06-16 10:20:05

多繼承C#

2009-08-07 13:03:10

C#控件數(shù)組

2009-08-17 17:16:19

C#實(shí)現(xiàn)在線升級(jí)

2010-01-14 18:00:07

VB.NET串行化對(duì)象

2009-11-17 16:24:27

PHP變量串行化

2009-09-02 13:22:23

C#組件化程序設(shè)計(jì)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)