C#组件开发(2),添加属性和事件

来源:互联网 发布:java调用golang 编辑:程序博客网 时间:2024/05/17 04:38

刚进门什么都不知道的请拐到下面这里

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窗口如下:
这里写图片描述
这里写图片描述

跟上一篇介绍一样, 建一个组件工程,把代码直接粘上就可以;

原创粉丝点击