C# 创建自定义事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9885437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a Custom Event
提问by whytheq
Can a custom event be created for any object method?
To do this do I just use the following syntax?:
可以为任何对象方法创建自定义事件吗?
为此,我只需使用以下语法?:
myObject.myMethod +=new EventHandler(myNameEvent);
The following code has prompted this question:
以下代码提示了这个问题:
private void btRunProcessAndRefresh_Click(object sender,EventArgs e)
{
myProcess =new Process();
myProcess.StartInfo.FileName = @"c:\ConsoleApplication4.exe";
myProcess.Exited += new EventHandler(MyProcessExited);
myProcess.EnableRaisingEvents =true;
myProcess.SynchronizingObject =this;
btRunProcessAndRefresh.Enabled =false;
myProcess.Start();
}
采纳答案by Pranay Rana
Yes you can do like this :
是的,你可以这样做:
Creating advanced C# custom events
or
或者
The Simplest C# Events Example Imaginable
public class Metronome
{
public event TickHandler Tick;
public EventArgs e = null;
public delegate void TickHandler(Metronome m, EventArgs e);
public void Start()
{
while (true)
{
System.Threading.Thread.Sleep(3000);
if (Tick != null)
{
Tick(this, e);
}
}
}
}
public class Listener
{
public void Subscribe(Metronome m)
{
m.Tick += new Metronome.TickHandler(HeardIt);
}
private void HeardIt(Metronome m, EventArgs e)
{
System.Console.WriteLine("HEARD IT");
}
}
class Test
{
static void Main()
{
Metronome m = new Metronome();
Listener l = new Listener();
l.Subscribe(m);
m.Start();
}
}
回答by MBen
You need to declare your event in the class from myObject :
您需要在 myObject 的类中声明您的事件:
public event EventHandler<EventArgs> myMethod; //you should name it as an event, like ObjectChanged.
then myNameEvent is the callback to handle the event, and it can be in any other class
那么 myNameEvent 是处理事件的回调,它可以在任何其他类中
回答by ionden
Declare the class containing the event:
声明包含事件的类:
class MyClass {
public event EventHandler MyEvent;
public void Method() {
OnEvent();
}
private void OnEvent() {
if (MyEvent != null) {
MyEvent(this, EventArgs.Empty);
}
}
}
Use it like this:
像这样使用它:
MyClass myObject = new MyClass();
myObject.MyEvent += new EventHandler(myObject_MyEvent);
myObject.Method();
回答by daryal
Yes you can create events on objects, here is an example;
是的,您可以在对象上创建事件,这是一个示例;
public class Foo
{
public delegate void MyEvent(object sender, object param);
event MyEvent OnMyEvent;
public Foo()
{
this.OnMyEvent += new MyEvent(Foo_OnMyEvent);
}
void Foo_OnMyEvent(object sender, object param)
{
if (this.OnMyEvent != null)
{
//do something
}
}
void RaiseEvent()
{
object param = new object();
this.OnMyEvent(this,param);
}
}
回答by Strillo
Yes, provided you have access to the object definition and can modify it to declare the custom event
是的,前提是您有权访问对象定义并可以对其进行修改以声明自定义事件
public event EventHandler<EventArgs> ModelChanged;
And normally you'd back this up with a private method used internally to invoke the event:
通常,您会使用内部使用的私有方法来调用该事件:
private void OnModelChanged(EventArgs e)
{
if (ModelChanged != null)
ModelChanged(this, e);
}
Your code simply declares a handler for the declared myMethodevent (you can also remove the constructor), which would get invoked every time the object triggers the event.
您的代码只是为声明的myMethod事件声明了一个处理程序(您也可以删除构造函数),每次对象触发事件时都会调用它。
myObject.myMethod += myNameEvent;
Similarly, you can detacha handler using
同样,您可以使用分离处理程序
myObject.myMethod -= myNameEvent;
Also, you can write your own subclass of EventArgsto provide specific data when your event fires.
此外,您可以编写自己的子类EventArgs以在事件触发时提供特定数据。
回答by Antoine C.
Based on @ionden's answer, the call to the delegate could be simplified using null propagationsince C# 6.0.
根据@ionden 的回答,从 C# 6.0 开始,可以使用空传播来简化对委托的调用。
Your code would simply be:
您的代码只是:
class MyClass {
public event EventHandler MyEvent;
public void Method() {
MyEvent?.Invoke(this, EventArgs.Empty);
}
}
Use it like this:
像这样使用它:
MyClass myObject = new MyClass();
myObject.MyEvent += new EventHandler(myObject_MyEvent);
myObject.Method();

