C#事件处理(相比于Java)
我目前有一个hardtime的理解和执行使用delagates在C#中的事件。我习惯做事Java的方式:
I am currently having a hardtime understanding and implementing events in C# using delagates. I am used to the Java way of doing things:
- 定义为一个监听器类型的接口,包含大量的方法定义
- 定义适配器类的界面,使事情变得更容易,如果我不感兴趣,在听众中定义的所有事件
- 定义添加,删除和获取[]中引发的事件
- 定义保护消防方法来做,通过添加侦听器列表循环和调用正确的方法的脏活类的方法
- Define an interface for a listener type which would contain a number of method definitions
- Define adapter class for that interface to make things easier if I'm not interested in all the events defined in a listener
- Define Add, Remove and Get[] methods in the class which raises the events
- Define protected fire methods to do the dirty work of looping through the list of added listeners and calling the correct method
这我理解(和喜欢!) - 我知道我能做到这一点正是在C#一样的,但似乎一个新的(更好?)系统是在放置的C#。阅人无数的教程说明在C#中使用委托和事件后,我仍然感到没有接近真正了解正在发生的事情:•
This I understand (and like!) - I know I could do this exactly the same in c#, but it seems that a new (better?) system is in place for c#. After reading countless tutorials explaining the use of delegates and events in c# I still am no closer to really understanding what is going on :S
总之,对于下面的方法我将如何实现事件系统在C#中:
In short, for the following methods how would I implement the event system in c#:
void computerStarted(Computer computer);
void computerStopped(Computer computer);
void computerReset(Computer computer);
void computerError(Computer computer, Exception error);
^上面的方法都从我试图端口超过我曾经做了一个Java应用程序采取为C#。
^ The above methods are taken from a Java application I once made which I'm trying to port over to c#.
很多很多的感谢!
您会创建四个事件和方法,以提高他们,一个新的基于EventArgs的类一起指示错误:
You'd create four events, and methods to raise them, along with a new EventArgs-based class to indicate the error:
public class ExceptionEventArgs : EventArgs
{
private readonly Exception error;
public ExceptionEventArgs(Exception error)
{
this.error = error;
}
public Error
{
get { return error; }
}
}
public class Computer
{
public event EventHandler Started = delegate{};
public event EventHandler Stopped = delegate{};
public event EventHandler Reset = delegate{};
public event EventHandler<ExceptionEventArgs> Error = delegate{};
protected void OnStarted()
{
Started(this, EventArgs.Empty);
}
protected void OnStopped()
{
Stopped(this, EventArgs.Empty);
}
protected void OnReset()
{
Reset(this, EventArgs.Empty);
}
protected void OnError(Exception e)
{
Error(this, new ExceptionEventArgs(e));
}
}
班会,然后订阅使用一个方法事件或匿名函数:
Classes would then subscribe to the event using either a method or a an anonymous function:
someComputer.Started += StartEventHandler; // A method
someComputer.Stopped += delegate(object o, EventArgs e)
{
Console.WriteLine("{0} has started", o);
};
someComputer.Reset += (o, e) => Console.WriteLine("{0} has been reset");
有几件事情需要注意上述:
A few things to note about the above:
- 的OnXXX方法保护,使派生类可以提高事件。这并不总是必要的 - 这样做,你认为合适
- 的
委托{}每个事件的声明
片只是一个。招避免做一个空检查。它的订阅无操作事件处理程序的每个事件 - 事件声明是的字段般的事件的。什么是真正被创造既是一个变量的和的事件。类在里面你看到变量;类的外部看到的事件。
- The OnXXX methods are protected so that derived classes can raise the events. This isn't always necessary - do it as you see fit.
- The
delegate{}
piece on each event declaration is just a trick to avoid having to do a null check. It's subscribing a no-op event handler to each event - The event declarations are field-like events. What's actually being created is both a variable and an event. Inside the class you see the variable; outside the class you see the event.
请参阅我的events/delegates 文章有关事件的更详细信息。
See my events/delegates article for much more detail on events.