C#組件開發(2),添加屬性和事件

剛進門什麼都不知道的請拐到下面這裏

C# 組件開發,簡介+ hello word 級別教程!


這篇分享PropertyAttribute和EventAttribute

EventAttribute有:

  1. BrowsableAttribute
  2. CategoryAttribute
  3. DescriptionAttribute
  4. DefaultEventAttribute

PropertyAttribute有:

  1. BrowsableAttribute
  2. CategoryAttribute
  3. DescriptionAttribute
  4. DefaultPropertyAttribute
  5. DefaultValueAttribute
  6. EditorAttribute
  7. DesignerSerializationVisibilityAttribute
  8. LocalizableAttribute
  9. TypeConverterAttribute
  10. BindableAttribute

這一小節主要講以上紅色的Attribute,再下章的Designer UI 會講藍色的Attribute,紫色的Attribute不做講解。

上述的Attribute 簡明闡明如下:

BrowsableAttribute:在Property窗口中是否可見。
CategoryAttribute:Property或者Event所屬的哪個組。
DescriptionAttribute:Property或者Event的簡單描述。
DefaultEventAttribute:默認Event。
DefaultPropertyAttribute:默認Property,選中組件,其Property窗口中默認選中在這個Property上。
DefaultValueAttribute: msdn中的說明爲:“可視化設計器可以使用默認值重置成員的值。代碼生成器也可使用默認值確定是否爲成員生成代碼” 。這個特性可以幫助IDE減少Code生成的工作,如果設計時某個標示有DefaultValueAttribute的Property的值和DefaultValue的值一樣,IDE將不會爲這個屬性生成代碼;否則,IDE會自動在InitializeComponent中添加的代碼。這個不是“創建Component時,對標示有DefaultValueAttribute的Property產生默認值”的意思。

個人風格:上例子, 在實踐中體會吧~

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Components
{
    // PropertyAttribute、EventAttribute分別放在Property、Event上,並[]括起來。
    // DefaultPropertyAttribute、DefaultEventAttribute必須放在類頭上。
    [DefaultEvent("CustomerLogout")]
    public class Customer : Component
    {
        private string _id;
        private string _sex;
        private int _age;
        private string _address;
        private DateTime _createTime;

        // 沒有CategoryAttribute、DescriptionAttribute。
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        // 此屬性在Customer's Details分組中,CategoryAttribute、DescriptionAttribute也適用於Event。
        [Category("Customer's Details"), Description("Customer's Sex")]  // 可以在一個[]裏寫兩個Attribute。
        public string Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }

        [Category("Customer's Details")]
        [Description("Customer's Age"), DefaultValue(20)]
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        [DefaultValue("shanghai"), Category("Customer's Details")]
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }

        [Browsable(false)] // 此Property在Property窗口中不可見,BrowsableAttribute也適用於Event。
        public DateTime CreateTime
        {
            get { return _createTime; }
            set { _createTime = value; }
        }



        public sealed class CustomerLoginEventArgs : EventArgs
        { }
        public sealed class CustomerLogoutEventArgs : EventArgs
        { }

        public delegate void CustomerLoginEventHandler(object sender, CustomerLoginEventArgs e);
        public delegate void CustomerLogoutEventHandler(object sender, CustomerLogoutEventArgs e);

        public event CustomerLoginEventHandler CustomerLogin
        {
            add { }
            remove { }
        }

        public event CustomerLogoutEventHandler CustomerLogout
        {
            add { }
            remove { }
        }
    }
}

其 Property、Event窗口如下:
這裏寫圖片描述
這裏寫圖片描述

跟上一篇介紹一樣, 建一個組件工程,把代碼直接粘上就可以;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章