如何派遣在C#中的事件 [英] How to dispatch events in C#

查看:137
本文介绍了如何派遣在C#中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自己的事件,并派遣他们。
我从来没有这样做之前,在C#中,只在Flex中。我想一定有很多differencies的。

I wish to create own events and dispatch them. I never done this before in C#, only in Flex.. I guess there must be a lot of differencies.

谁能给我一个很好的例子?

Can anyone provide me a good example?

推荐答案

有是在所有的类库使用的模式。推荐您自己的类也一样,尤其是对于框架/库code。但没有人会阻止你,当你偏离或跳过了几步。

There is a pattern that is used in all library classes. It is recommended for your own classes too, especially for framework/library code. But nobody will stop you when you deviate or skip a few steps.

下面是基于最简单的事件委托的示意图, System.Eventhandler

Here is a schematic based on the simplest event-delegate, System.Eventhandler.

// The delegate type. This one is already defined in the library, in the System namespace
// the `void (object, EventArgs)` signature is also a recommended pattern
public delegate void Eventhandler(object sender, Eventargs args);  

// your publishing class
class Foo  
{
    public event EventHandler Changed;    // the Event

    protected virtual void OnChanged()    // the Trigger method, called to raise the event
    {
        // make a copy to be more thread-safe
        EventHandler handler = Changed;   

        if (handler != null)
        {
            // invoke the subscribed event-handler(s)
            handler(this, EventArgs.Empty);  
        }
    }

    // an example of raising the event
    void SomeMethod()
    {
       if (...)        // on some condition
         OnChanged();  // raise the event
    }
}

和如何使用它:

// your subscribing class
class Bar
{       
    public Bar()
    {
        Foo f = new Foo();
        f.Changed += Foo_Changed;    // Subscribe, using the short notation
    }

    // the handler must conform to the signature
    void Foo_Changed(object sender, EventArgs args)  // the Handler (reacts)
    {
        // the things Bar has to do when Foo changes
    }
}


当你拥有了信息传递下去:


And when you have information to pass along:

class MyEventArgs : EventArgs    // guideline: derive from EventArgs
{
    public string Info { get; set; }
}

class Foo  
{
    public event EventHandler<MyEventArgs> Changed;    // the Event
    ...
    protected virtual void OnChanged(string info)      // the Trigger
    {
        EventHandler handler = Changed;   // make a copy to be more thread-safe
        if (handler != null)
        {
           var args = new MyEventArgs(){Info = info};  // this part will vary
           handler(this, args);  
        }
    }
}

class Bar
{       
   void Foo_Changed(object sender, MyEventArgs args)  // the Handler
   {
       string s = args.Info;
       ...
   }
}


修改

在C#6中的触发方法调用code变得轻松了许多,空试验可以用空条件运算缩短不进行复制:

In C# 6 the calling code in the 'Trigger' method becomes a lot easier, the null test can be shortened with the null-conditional operator ?. without making a copy:

protected virtual void OnChanged(string info)      // the Trigger
{
    var args = new MyEventArgs(){Info = info};  // this part will vary
    Changed?.Invoke(this, args);
}

这篇关于如何派遣在C#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆