Events基本概念----Beginning Visual C#_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Events基本概念----Beginning Visual C#

Events基本概念----Beginning Visual C#

 2014/12/2 12:26:59  grass of moon  程序员俱乐部  我要评论(0)
  • 摘要:span.kw{color:#007020;font-weight:bold;}code>span.dt{color:#902000;}code>span.dv{color:#40a070;}code>span.bn{color:#40a070;}code>span.fl{color:#40a070;}code>span.ch{color:#4070a0;}code>span.st{color:#4070a0;}code>span.co{color:#60a0b0;font-style
  • 标签:Visua Visual C# 基本概念 概念

一个事件的发生需要有触发条件,如定时发生,鼠标点击等,以及即将发生的事件本身,因此,events相关的最简单的示例如下:

class="sourceCode cs">// 时间触发器
class TimerTrigger
{
    private Timer myTimer;

    public TimerTrigger() 
    {
        myTimer = new Timer(100);
    }
    
    public TimerTrigger(int timeInterval)
    {
        myTimer = new Timer(timeInterval);
    }

    // 向时间触发器添加具事件的handler
    public void AddEventHander(ElapsedEventHandler source)
    {
        myTimer.Elapsed += source;
    }

    public void Start()
    {
        myTimer.Start();
    }

    public void Stop()
    {
        myTimer.Stop();
    }
}

class Program
{
    static int counter = 0;
    static string displayString = 
        "This string will appear one letter at a time. ";

    // 即将被添加到时间触发器的事件
    static void WriteChar(object source, ElapsedEventArgs e)
    {
        Console.Write(displayString[counter++ % displayString.Length]);
    }

    public static void Main()
    {
        TimerTrigger timerTrigger = new TimerTrigger(100);
        timerTrigger.AddEventHander(new ElapsedEventHandler(WriteChar));
        timerTrigger.Start();
        Console.ReadLine();
    }
}

在上面的例子的main函数中,直接使用了c#自带的ElapsedEventHandler delegated类型,那么如何自定义事件处理函数呢?具体如下:

// 自定义时间处理代表
public delegate void MessageHandler(string message);

class TimerTrigger
{
    private Timer myTimer;
    private event MessageHandler MessageArrived;

    public TimerTrigger(int timeInterval)
    {
        myTimer = new Timer(timeInterval);
        myTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
    }

    public void AddEventHandler(MessageHandler source)
    {
        MessageArrived += source; 
    }

    public void Start()
    {
        myTimer.Start();
    }

    public void Stop()
    {
        myTimer.Stop();
    }

    private void CheckForMessage(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Checking for new messages.");
        if (MessageArrived != null)
        {
            MessageArrived("hello world");
        }

    }
}

class Program
{
    static int counter = 0;

    static void WriteFirstMessage(string message)
    {
        Console.WriteLine("The First {0} number: {1}", message, counter++);
    }

    static void WriteSecondMessage(string message)
    {
        Console.WriteLine("The Second {0} number: {1}", message, counter++);
    }

    public static void Main()
    {
        TimerTrigger timerTrigger = new TimerTrigger(100);
        timerTrigger.AddEventHandler(new MessageHandler(WriteFirstMessage));
        timerTrigger.AddEventHandler(new MessageHandler(WriteSecondMessage));
        timerTrigger.Start();
        Console.ReadLine();
    }
}

在上述的例子中,将消息传递给处理事件所用的函数为WriteFirstMessage(string message),其传递的参数为string类型的。再考虑道<Timer>.ElapsedEventHandler所代表的函数的传递参数为:(object source, ElapsedEventArgs e)两个参数,那么我们能否实现类似的形式呢?这是可以的,如下:

public class MessageArrivedEventArgs : EventArgs
{
    private string message;
    public string Message
    {
        get
        {
            return message;
        }
    }
    public MessageArrivedEventArgs()
    {
        message = "No message sent.";
    }
    public MessageArrivedEventArgs(string newMessage)
    {
        message = newMessage;
    }
}
         
public delegate void MessageHandler(TimerTrigger source,
    MessageArrivedEventArgs e);

public class TimerTrigger
{
    private Timer myTimer;
    private event MessageHandler MessageArrived;

    public TimerTrigger(int timeInterval)
    {
        myTimer = new Timer(timeInterval);
        myTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
    }

    public void AddEventHandler(MessageHandler source)
    {
        MessageArrived += source; 
    }

    public void Start()
    {
        myTimer.Start();
    }

    public void Stop()
    {
        myTimer.Stop();
    }

    private void CheckForMessage(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Checking for new messages.");
        if (MessageArrived != null)
        {
            MessageArrived(this, new MessageArrivedEventArgs("hello world"));
        }

    }
}

class Program
{
    static int counter = 0;

    static void WriteFirstMessage(TimerTrigger source, MessageArrivedEventArgs e)
    {
        Console.WriteLine("The First {0} number: {1}", e.Message, counter++);
    }

    static void WriteSecondMessage(TimerTrigger source, MessageArrivedEventArgs e)
    {
        Console.WriteLine("The Second {0} number: {1}", e.Message, counter++);
    }

    public static void Main()
    {
        TimerTrigger timerTrigger = new TimerTrigger(100);
        timerTrigger.AddEventHandler(new MessageHandler(WriteFirstMessage));
        timerTrigger.AddEventHandler(new MessageHandler(WriteSecondMessage));
        timerTrigger.Start();
        Console.ReadLine();
    }
}
上一篇: 微软收购电邮客户端Acompli 价格超2亿美元 下一篇: 没有下一篇了!
发表评论
用户名: 匿名