欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c#事件委托

程序员文章站 2022-07-16 18:54:28
...
代码是理解的亲娘   直接开干   
using System;
using System.Collections;
public delegate void AddEventHandler(object sender, EventArgs e);  //声明一个事件委托类型
public class ListWithEvent : ArrayList
{
    public event AddEventHandler Added;    //声明一个事件
    protected virtual void OnChanged(EventArgs e)
    {
        if (Added != null)
            Added(this, e);
    }
    public override int Add(object value)
    {
        int i = base.Add(value);
        OnChanged(EventArgs.Empty);
        return i;
    }
}
class EventMethod
{
    public void ListChanged(object sender, EventArgs e)
    {
        Console.WriteLine("This is called when the event fires.");
    }
}
class Program
{
    public static void Main()
    {
        ListWithEvent List = new ListWithEvent();
        EventMethod test = new EventMethod();
        List.Added += new AddEventHandler(test.ListChanged);
        List.Add("Item 1");
        List.Add("Item 2");
        Console.Read();
    }
}

c#事件委托


懂了么?

不管,继续


using System;
class Publisher
{
    public delegate void Publish(); //修饰符  关键字    类型   委托名
    public event Publish OnPublish;   //定义Pubish委托类型事件
    public void issue()     //定义触发事件的方法
    {
        if (OnPublish != null)        //事件不为空
        {
            Console.WriteLine("发行刊物!");
            OnPublish();             //事件发生
        }
    }
}
class Subscriber
{
    public void Receive()
    {
        Console.WriteLine("订阅者已收到刊物!");
    }
}
class Story
{
    static void Main()
    {
        Publisher Pub = new Publisher();
        Subscriber Sub = new Subscriber();
        Pub.OnPublish += new Publisher.Publish(Sub.Receive); //此时往委托的对象->事件里存放一个新的委托对象(即事件);此时该事件不为空,参数是目标对象的方法
        Pub.issue();   //触发事件                                               
        Console.Read();
    }
}

c#事件委托


哇哈哈!  没事 

来吧!

using System;
class Publisher    //出版社
{
    public delegate void PubComputer(string magazineName);
    public delegate void PubLife(string magzineName);
    public event PubComputer OnPubComputer;
    public event PubLife OnPubLife;
    public void issueComputer()   //定义触发事件的方法
    {
        if (OnPubComputer != null)       
        {
            Console.WriteLine("发行《电脑》杂志!");
            OnPubComputer("电脑杂志");   //事件发生   利用委托传递参数
        }
    }
    public void issureLife()   //定义触发事件的方法
    {
        if (OnPubLife != null)
        {
            Console.WriteLine("发行《生活》杂志!");
            OnPubLife("生活杂志");   //事件发生
        }
    }
}
class Subscriber      //订阅者
{
    private string name;
    public Subscriber(string name)
    {
        this.name = name;
    }
    public void Receive(string magazineName)
    {
        Console.WriteLine(name + "已经收到" + magazineName);
    }
}
class Story
{
    static void Main()
    {
        Publisher Pub = new Publisher();
        Subscriber zs = new Subscriber("张三");
        Pub.OnPubComputer += new Publisher.PubComputer(zs.Receive); //向事件发行者预定一个事件
        Subscriber ls = new Subscriber("李四");
        Pub.OnPubComputer += new Publisher.PubComputer(ls.Receive);  //触发事件  引发与之关联的方法
        Pub.OnPubLife += new Publisher.PubLife(ls.Receive);//new操作符是创建一个委托的对象
                            //此时往委托的对象->事件里存放一个新的委托对象(即事件),与目标对象方法关联;此时该事件不为空  OnPublife!=null
Pub.issueComputer(); //触发事件 Pub.issureLife(); Console.WriteLine(); Console.WriteLine("一年以后"); Pub.OnPubComputer -= new Publisher.PubComputer(ls.Receive); Pub.issueComputer(); Pub.issureLife(); Console.Read(); }}

c#事件委托



现在好点了吧


再来大招!!!

using System;
using System.Collections;
class PubEventArgs : EventArgs
{
    private readonly string m_magazineName;
    private readonly string m_pubDate;
    public PubEventArgs(string magazineName, DateTime PubDate)
    {
        m_magazineName = magazineName;
        //PubDate = new DateTime();
        m_pubDate = PubDate.ToString();
    }
    public string magazineName   //属性
    {
        get
        {
            return m_magazineName;
        }
    }
    public string pubDate    //属性
    {
        get
        {
            return m_pubDate;
        }
    }
}
class Publisher
{
    public delegate void PubComputerEventHandler(object sender, PubEventArgs e);
    public delegate void PubLifeEventHandler(object sender, PubEventArgs e);     
    public event PubComputerEventHandler PubComputer;         
    public event PubLifeEventHandler PubLife;                         
    protected virtual void OnPubComputer(PubEventArgs e)            
    {                                                                  
        PubComputerEventHandler handler = PubComputer; //防止可能出现的线程同步问题  
        if (handler != null)    //当事件绑定对应的方法时,该事件非空                    
        {                                                                          
            handler(this, e);   //this  为触发该事件的类的对象    e 为包含事件参数的类的对象      
        }                                                                                    
    }                                                                                    
    protected virtual void OnPubLife(PubEventArgs e)   
    {
        PubLifeEventHandler handler = PubLife;  //与上同
        if (handler != null)
        {
            handler(this, e);
        }
    }
    public void issueComputer(string magazineName, DateTime pubDate)
    {
        Console.WriteLine("发行" + magazineName);
        OnPubComputer(new PubEventArgs(magazineName, pubDate));
    }
    public void issueLife(string magazineName, DateTime pubDate)
    {
        Console.WriteLine("发行" + magazineName);
        OnPubLife(new PubEventArgs(magazineName, pubDate));
    }
}
class Subscriber
{
    private string name;
    public Subscriber(string name)
    {
        this.name = name;
    }
    public void Receive(object sender, PubEventArgs e)
    {
        Console.WriteLine(e.pubDate + "" + name + "已经收到" + e.magazineName);
    }
}
class Story
{
    static void Main()
    {
        Publisher Pub = new Publisher();
        Subscriber zs = new Subscriber("张三");
        Pub.PubComputer += new Publisher.PubComputerEventHandler(zs.Receive);
        Subscriber ls = new Subscriber("李四");
        Pub.PubComputer += new Publisher.PubComputerEventHandler(ls.Receive);
        Pub.PubLife += new Publisher.PubLifeEventHandler(ls.Receive);
        Pub.issueComputer("电脑杂志", Convert.ToDateTime("2010-9-15"));
        Pub.issueLife("生活杂志", Convert.ToDateTime("2010-9-15"));
        Console.WriteLine();
        Console.WriteLine("一年以后");
        Pub.PubComputer -= new Publisher.PubComputerEventHandler(ls.Receive);
        Pub.issueComputer("电脑杂志", Convert.ToDateTime("2011-9-15"));
        Pub.issueLife("生活杂志", Convert.ToDateTime("2011-9-15"));
        Console.Read();
    }
}

c#事件委托



.net Framework 的命名方式  1、事件的命名应使用Pascal Casing 命名方式
   2 声明delegate时,必须使用void类型作为返回值。事件命名方面 例如: eventName事件的事件委托是
EventNameEventHandler,时间接受两个传入参数,一律命名为sender与e。
   3 定义一个提供事件数据的类,对类以EventNameEventArgs 进行命名,从System.EventArgs派生该类,然后添加所有事件特定的成员  
   4 在类中实现时间准则。如果事件不需要传递参数,也要声明两个参数,e
         参数直接使用系统提供的System.EventArgs类。如果传递数据,则要从 System.EventArgs继承一个类,并把数据放在里面。
   5 在引发事件的类中,提供一个受保护的办法。以OnEventName进行命名,在该方法中引发事件。