我理解事件的目的,特别是在创建用户界面的上下文中。我认为这是创建事件的原型:
public void EventName(object sender, EventArgs e);
事件处理程序做什么,为什么需要它们,以及如何创建一个?
我理解事件的目的,特别是在创建用户界面的上下文中。我认为这是创建事件的原型:
public void EventName(object sender, EventArgs e);
事件处理程序做什么,为什么需要它们,以及如何创建一个?
当前回答
c#有两个术语,委托和事件。让我们从第一个开始。
委托
委托是对方法的引用。就像你可以创建一个实例的引用:
MyClass instance = myFactory.GetInstance();
你可以使用委托来创建一个方法的引用:
Action myMethod = myFactory.GetInstance;
现在你有了一个方法的引用,你可以通过引用调用这个方法:
MyClass instance = myMethod();
但你为什么要这么做?你也可以直接调用myFactory.GetInstance()。在这种情况下你可以。但是,有很多情况需要考虑,您不希望应用程序的其余部分了解myFactory或直接调用myFactory. getinstance()。
一个明显的例子是,如果你想从一个中心位置(也就是工厂方法模式)将myFactory.GetInstance()替换为myOfflineFakeFactory.GetInstance()。
工厂方法模式
所以,如果你有一个TheOtherClass类,它需要使用myFactory. getinstance(),这是没有委托的代码看起来的样子(你需要让TheOtherClass知道你的myFactory的类型):
TheOtherClass toc;
//...
toc.SetFactory(myFactory);
class TheOtherClass
{
public void SetFactory(MyFactory factory)
{
// set here
}
}
如果你使用委托,你不需要暴露我的工厂的类型:
TheOtherClass toc;
//...
Action factoryMethod = myFactory.GetInstance;
toc.SetFactoryMethod(factoryMethod);
class TheOtherClass
{
public void SetFactoryMethod(Action factoryMethod)
{
// set here
}
}
因此,您可以将委托交给其他类使用,而无需向它们暴露您的类型。你唯一要暴露的是你的方法的签名(你有多少参数等等)。
"我的方法的签名"我在哪儿听过这个?哦,是的,接口!!接口描述了整个类的签名。把委托看作是只描述一个方法的签名!
接口和委托之间的另一个巨大区别是,当你编写类时,你不必对c#说“这个方法实现了那种类型的委托”。对于接口,你确实需要说“这个类实现了那种类型的接口”。
此外,一个委托引用可以(有一些限制,见下文)引用多个方法(称为MulticastDelegate)。这意味着当您调用委托时,将执行多个显式附加的方法。一个对象引用总是只能引用一个对象。
MulticastDelegate的限制是(方法/委托)签名不应该有任何返回值(void),关键字out和ref不能在签名中使用。显然,您不能调用两个返回一个数字的方法,并期望它们返回相同的数字。一旦签名符合要求,该委托就自动成为MulticastDelegate。
事件
事件只是属性(就像get;set;属性到实例字段),这将从其他对象向委托公开订阅。但是,这些属性不支持get;set;。相反,它们支持add;删除;
所以你可以有:
Action myField;
public event Action MyProperty
{
add { myField += value; }
remove { myField -= value; }
}
在UI中的使用(WinForms,WPF,UWP等等)
So, now we know that a delegate is a reference to a method and that we can have an event to let the world know that they can give us their methods to be referenced from our delegate, and we are a UI button, then: we can ask anyone who is interested in whether I was clicked, to register their method with us (via the event we exposed). We can use all those methods that were given to us and reference them by our delegate. And then, we'll wait and wait.... until a user comes and clicks on that button, then we'll have enough reason to invoke the delegate. And because the delegate references all those methods given to us, all those methods will be invoked. We don't know what those methods do, nor we know which class implements those methods. All we do care about is that someone was interested in us being clicked, and gave us a reference to a method that complied with our desired signature.
Java
像Java这样的语言没有委托。相反,他们使用接口。他们这样做的方式是让任何对“我们被点击”感兴趣的人来实现某个接口(使用我们可以调用的某个方法),然后给我们实现该接口的整个实例。我们保留了实现该接口的所有对象的列表,并在我们被单击时调用它们的“我们可以调用的特定方法”。
其他回答
//This delegate can be used to point to methods
//which return void and take a string.
public delegate void MyDelegate(string foo);
//This event can cause any method which conforms
//to MyEventHandler to be called.
public event MyDelegate MyEvent;
//Here is some code I want to be executed
//when SomethingHappened fires.
void MyEventHandler(string foo)
{
//Do some stuff
}
//I am creating a delegate (pointer) to HandleSomethingHappened
//and adding it to SomethingHappened's list of "Event Handlers".
myObj.MyEvent += new MyDelegate (MyEventHandler);
我同意KE50的观点,除了我认为'event'关键字是'ActionCollection'的别名,因为事件包含了要执行的操作的集合(例如。委托)。
using System;
namespace test{
class MyTestApp{
//The Event Handler declaration
public delegate void EventAction();
//The Event Action Collection
//Equivalent to
// public List<EventAction> EventActions=new List<EventAction>();
//
public event EventAction EventActions;
//An Action
public void Hello(){
Console.WriteLine("Hello World of events!");
}
//Another Action
public void Goodbye(){
Console.WriteLine("Goodbye Cruel World of events!");
}
public static void Main(){
MyTestApp TestApp = new MyTestApp();
//Add actions to the collection
TestApp.EventActions += TestApp.Hello;
TestApp.EventActions += TestApp.Goodbye;
//Invoke all event actions
if (TestApp.EventActions!= null){
//this peculiar syntax hides the invoke
TestApp.EventActions();
//using the 'ActionCollection' idea:
// foreach(EventAction action in TestApp.EventActions)
// action.Invoke();
}
}
}
}
我最近做了一个如何在c#中使用事件的例子,并发布在我的博客上。我用了一个简单的例子,尽量把它讲清楚。如果它可能帮助任何人,这里是:http://www.konsfik.com/using-events-in-csharp/
它包括描述和源代码(带有大量注释),主要关注事件和事件处理程序的正确使用(类似模板)。
一些要点是:
Events are like "sub - types of delegates", only more constrained (in a good way). In fact an event's declaration always includes a delegate (EventHandlers are a type of delegate). Event Handlers are specific types of delegates (you may think of them as a template), which force the user to create events which have a specific "signature". The signature is of the format: (object sender, EventArgs eventarguments). You may create your own sub-class of EventArgs, in order to include any type of information the event needs to convey. It is not necessary to use EventHandlers when using events. You may completely skip them and use your own kind of delegate in their place. One key difference between using events and delegates, is that events can only be invoked from within the class that they were declared in, even though they may be declared as public. This is a very important distinction, because it allows your events to be exposed so that they are "connected" to external methods, while at the same time they are protected from "external misuse".
这实际上是一个事件处理程序的声明——一个在触发事件时被调用的方法。要创建一个事件,你可以这样写:
public class Foo
{
public event EventHandler MyEvent;
}
然后你可以像这样订阅这个事件:
Foo foo = new Foo();
foo.MyEvent += new EventHandler(this.OnMyEvent);
OnMyEvent()的定义如下:
private void OnMyEvent(object sender, EventArgs e)
{
MessageBox.Show("MyEvent fired!");
}
每当Foo触发MyEvent,那么你的OnMyEvent处理程序将被调用。
你不必总是使用EventArgs的实例作为第二个参数。如果你想包含额外的信息,你可以使用一个从EventArgs派生的类(根据惯例EventArgs是基类)。例如,如果你查看WinForms中的Control上定义的一些事件,或者WPF中的FrameworkElement上定义的一些事件,你可以看到将附加信息传递给事件处理程序的事件示例。
我对这些事件的理解是;
委托:
保存要执行的方法/方法的引用的变量。这使得像传递变量一样传递方法成为可能。
创建和调用事件的步骤:
事件是委托的实例 由于事件是委托的实例,因此我们必须首先定义委托。 指定在事件触发时执行的方法/方法(调用委托) 触发事件(调用委托)
例子:
using System;
namespace test{
class MyTestApp{
//The Event Handler declaration
public delegate void EventHandler();
//The Event declaration
public event EventHandler MyHandler;
//The method to call
public void Hello(){
Console.WriteLine("Hello World of events!");
}
public static void Main(){
MyTestApp TestApp = new MyTestApp();
//Assign the method to be called when the event is fired
TestApp.MyHandler = new EventHandler(TestApp.Hello);
//Firing the event
if (TestApp.MyHandler != null){
TestApp.MyHandler();
}
}
}
}