如何在C#中调度事件

我希望创build自己的事件并发送它们。 我从来没有这样做过,在C#中,只有在Flex ..我想必须有很多differencies。

谁能为我提供一个很好的例子?

所有图书馆类都有一个模式。 它也推荐给你自己的类,特别是框架/库代码。 但是当你偏离或跳过几个步骤时,没有人会阻止你。

这里是一个基于最简单的事件委托, System.Eventhandler的原理图。

 // The delegate type. This one is already defined in the library, in the System namespace // the `void (object, EventArgs)` signature is also the 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 } } 

当你有信息传递:

 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开始,'Trigger'方法中的调用代码已经变得更容易了,nulltesting可以用null条件运算符来缩短?. 在保持线程安全的同时不需要复制:

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

C#中的事件使用委托。

 public static event EventHandler<EventArgs> myEvent; static void Main() { //add method to be called myEvent += Handler; //call all methods that have been added to the event myEvent(this, EventArgs.Empty); } static void Handler(object sender, EventArgs args) { Console.WriteLine("Event Handled!"); } 

使用典型的.NET事件模式,并假设您不需要任何特殊的参数。 你典型的事件和调度模式是这样的。

 public class MyClassWithEvents { public event EventHandler MyEvent; protected void OnMyEvent(object sender, EventArgs eventArgs) { if (MyEvent != null) { MyEvent(sender, eventArgs); } } public void TriggerMyEvent() { OnMyEvent(sender, eventArgs); } } 

将事情绑定到事件中可以像下面这样简单:

 public class Program { public static void Main(string[] args) { MyClassWithEvents obj = new MyClassWithEvents(); obj.MyEvent += obj_myEvent; } private static void obj_myEvent(object sender, EventArgs e) { //Code called when my event is dispatched. } } 

看看这个MSDN页面上的链接

看看“代表”。

  • 定义一个委托
  • 使用委托types作为字段/属性(添加“事件”关键字)
  • 您现在正在公开用户可以使用“+ = MyEventMethod”挂钩的事件

希望这可以帮助,