How can I make my own event in C#?
如何在c#中创建自己的事件?
4 个解决方案
#1
181
Here's an example of creating and using an event with C#
下面是使用c#创建和使用事件的示例
using System;
namespace Event_Example
{
//First we have to define a delegate that acts as a signature for the
//function that is ultimately called when the event is triggered.
//You will notice that the second parameter is of MyEventArgs type.
//This object will contain information about the triggered event.
public delegate void MyEventHandler(object source, MyEventArgs e);
//This is a class which describes the event to the class that recieves it.
//An EventArgs class must always derive from System.EventArgs.
public class MyEventArgs : EventArgs
{
private string EventInfo;
public MyEventArgs(string Text)
{
EventInfo = Text;
}
public string GetInfo()
{
return EventInfo;
}
}
//This next class is the one which contains an event and triggers it
//once an action is performed. For example, lets trigger this event
//once a variable is incremented over a particular value. Notice the
//event uses the MyEventHandler delegate to create a signature
//for the called function.
public class MyClass
{
public event MyEventHandler OnMaximum;
private int i;
private int Maximum = 10;
public int MyValue
{
get
{
return i;
}
set
{
if(value <= Maximum)
{
i = value;
}
else
{
//To make sure we only trigger the event if a handler is present
//we check the event to make sure it's not null.
if(OnMaximum != null)
{
OnMaximum(this, new MyEventArgs("You've entered " +
value.ToString() +
", but the maximum is " +
Maximum.ToString()));
}
}
}
}
}
class Program
{
//This is the actual method that will be assigned to the event handler
//within the above class. This is where we perform an action once the
//event has been triggered.
static void MaximumReached(object source, MyEventArgs e)
{
Console.WriteLine(e.GetInfo());
}
static void Main(string[] args)
{
//Now lets test the event contained in the above class.
MyClass MyObject = new MyClass();
MyObject.OnMaximum += new MyEventHandler(MaximumReached);
for(int x = 0; x <= 15; x++)
{
MyObject.MyValue = x;
}
Console.ReadLine();
}
}
}
#2
51
I have a full discussion of events and delegates in my events article. For the simplest kind of event, you can just declare a public event and the compiler will create both an event and a field to keep track of subscribers:
在我的活动文章中,我对事件和代表进行了充分的讨论。对于最简单的事件,您可以声明一个公共事件,而编译器将创建一个事件和一个字段来跟踪订阅者:
public event EventHandler Foo;
If you need more complicated subscription/unsubscription logic, you can do that explicitly:
如果您需要更复杂的订阅/取消订阅逻辑,您可以明确地这样做:
public event EventHandler Foo
{
add
{
// Subscription logic here
}
remove
{
// Unsubscription logic here
}
}
#3
20
You can declare an event with the following code:
您可以使用以下代码声明一个事件:
public event EventHandler MyOwnEvent;
A custom delegate type instead of EventHandler can be used if needed.
如果需要,可以使用自定义委托类型而不是EventHandler。
You can find detailed information/tutorials on the use of events in .NET in the article Events Tutorial (MSDN).
您可以在文章events教程(MSDN)中找到关于在. net中使用事件的详细信息/教程。
#4
3
to do it we have to know the three components
要做到这一点,我们必须知道这三个组成部分
- the place responsible for
firing the Event
- 负责发射事件的地方
- the place responsible for
responding to the Event
- 负责响应事件的地方
-
the Event itself
事件本身
a. Event
一个事件。
b .EventArgs
b .EventArgs
c. EventArgs enumeration
c . EventArgs枚举
now lets create Event that fired when a function is called
现在让我们创建在调用函数时触发的事件
but I my order of solving this problem like this: I'm using the class before I create it
但是我的解决这个问题的顺序是:我在创建它之前使用这个类。
-
the place responsible for
responding to the Event
负责响应事件的地方
NetLog.OnMessageFired += delegate(object o, MessageEventArgs args) { // when the Event Happened I want to Update the UI // this is WPF Window (WPF Project) this.Dispatcher.Invoke(() => { LabelFileName.Content = args.ItemUri; LabelOperation.Content = args.Operation; LabelStatus.Content = args.Status; }); };
NetLog is a static class I will Explain it later
NetLog是一个静态类,稍后我将对此进行解释
the next step is
下一个步骤是
-
the place responsible for
firing the Event
负责发射事件的地方
//this is the sender object, MessageEventArgs Is a class I want to create it and Operation and Status are Event enums NetLog.FireMessage(this, new MessageEventArgs("File1.txt", Operation.Download, Status.Started)); downloadFile = service.DownloadFile(item.Uri); NetLog.FireMessage(this, new MessageEventArgs("File1.txt", Operation.Download, Status.Finished));
the third step
第三步
- the Event itself
- 事件本身
I warped The Event within a class called NetLog
我在一个名为NetLog的类中扭曲了事件
public sealed class NetLog
{
public delegate void MessageEventHandler(object sender, MessageEventArgs args);
public static event MessageEventHandler OnMessageFired;
public static void FireMessage(Object obj,MessageEventArgs eventArgs)
{
if (OnMessageFired != null)
{
OnMessageFired(obj, eventArgs);
}
}
}
public class MessageEventArgs : EventArgs
{
public string ItemUri { get; private set; }
public Operation Operation { get; private set; }
public Status Status { get; private set; }
public MessageEventArgs(string itemUri, Operation operation, Status status)
{
ItemUri = itemUri;
Operation = operation;
Status = status;
}
}
public enum Operation
{
Upload,Download
}
public enum Status
{
Started,Finished
}
this class now contain the Event
, EventArgs
and EventArgs Enums
and the function
responsible for firing the event
这个类现在包含事件、EventArgs和EventArgs Enums以及负责触发事件的函数
sorry for this long answer
抱歉这么长时间的回答
#1
181
Here's an example of creating and using an event with C#
下面是使用c#创建和使用事件的示例
using System;
namespace Event_Example
{
//First we have to define a delegate that acts as a signature for the
//function that is ultimately called when the event is triggered.
//You will notice that the second parameter is of MyEventArgs type.
//This object will contain information about the triggered event.
public delegate void MyEventHandler(object source, MyEventArgs e);
//This is a class which describes the event to the class that recieves it.
//An EventArgs class must always derive from System.EventArgs.
public class MyEventArgs : EventArgs
{
private string EventInfo;
public MyEventArgs(string Text)
{
EventInfo = Text;
}
public string GetInfo()
{
return EventInfo;
}
}
//This next class is the one which contains an event and triggers it
//once an action is performed. For example, lets trigger this event
//once a variable is incremented over a particular value. Notice the
//event uses the MyEventHandler delegate to create a signature
//for the called function.
public class MyClass
{
public event MyEventHandler OnMaximum;
private int i;
private int Maximum = 10;
public int MyValue
{
get
{
return i;
}
set
{
if(value <= Maximum)
{
i = value;
}
else
{
//To make sure we only trigger the event if a handler is present
//we check the event to make sure it's not null.
if(OnMaximum != null)
{
OnMaximum(this, new MyEventArgs("You've entered " +
value.ToString() +
", but the maximum is " +
Maximum.ToString()));
}
}
}
}
}
class Program
{
//This is the actual method that will be assigned to the event handler
//within the above class. This is where we perform an action once the
//event has been triggered.
static void MaximumReached(object source, MyEventArgs e)
{
Console.WriteLine(e.GetInfo());
}
static void Main(string[] args)
{
//Now lets test the event contained in the above class.
MyClass MyObject = new MyClass();
MyObject.OnMaximum += new MyEventHandler(MaximumReached);
for(int x = 0; x <= 15; x++)
{
MyObject.MyValue = x;
}
Console.ReadLine();
}
}
}
#2
51
I have a full discussion of events and delegates in my events article. For the simplest kind of event, you can just declare a public event and the compiler will create both an event and a field to keep track of subscribers:
在我的活动文章中,我对事件和代表进行了充分的讨论。对于最简单的事件,您可以声明一个公共事件,而编译器将创建一个事件和一个字段来跟踪订阅者:
public event EventHandler Foo;
If you need more complicated subscription/unsubscription logic, you can do that explicitly:
如果您需要更复杂的订阅/取消订阅逻辑,您可以明确地这样做:
public event EventHandler Foo
{
add
{
// Subscription logic here
}
remove
{
// Unsubscription logic here
}
}
#3
20
You can declare an event with the following code:
您可以使用以下代码声明一个事件:
public event EventHandler MyOwnEvent;
A custom delegate type instead of EventHandler can be used if needed.
如果需要,可以使用自定义委托类型而不是EventHandler。
You can find detailed information/tutorials on the use of events in .NET in the article Events Tutorial (MSDN).
您可以在文章events教程(MSDN)中找到关于在. net中使用事件的详细信息/教程。
#4
3
to do it we have to know the three components
要做到这一点,我们必须知道这三个组成部分
- the place responsible for
firing the Event
- 负责发射事件的地方
- the place responsible for
responding to the Event
- 负责响应事件的地方
-
the Event itself
事件本身
a. Event
一个事件。
b .EventArgs
b .EventArgs
c. EventArgs enumeration
c . EventArgs枚举
now lets create Event that fired when a function is called
现在让我们创建在调用函数时触发的事件
but I my order of solving this problem like this: I'm using the class before I create it
但是我的解决这个问题的顺序是:我在创建它之前使用这个类。
-
the place responsible for
responding to the Event
负责响应事件的地方
NetLog.OnMessageFired += delegate(object o, MessageEventArgs args) { // when the Event Happened I want to Update the UI // this is WPF Window (WPF Project) this.Dispatcher.Invoke(() => { LabelFileName.Content = args.ItemUri; LabelOperation.Content = args.Operation; LabelStatus.Content = args.Status; }); };
NetLog is a static class I will Explain it later
NetLog是一个静态类,稍后我将对此进行解释
the next step is
下一个步骤是
-
the place responsible for
firing the Event
负责发射事件的地方
//this is the sender object, MessageEventArgs Is a class I want to create it and Operation and Status are Event enums NetLog.FireMessage(this, new MessageEventArgs("File1.txt", Operation.Download, Status.Started)); downloadFile = service.DownloadFile(item.Uri); NetLog.FireMessage(this, new MessageEventArgs("File1.txt", Operation.Download, Status.Finished));
the third step
第三步
- the Event itself
- 事件本身
I warped The Event within a class called NetLog
我在一个名为NetLog的类中扭曲了事件
public sealed class NetLog
{
public delegate void MessageEventHandler(object sender, MessageEventArgs args);
public static event MessageEventHandler OnMessageFired;
public static void FireMessage(Object obj,MessageEventArgs eventArgs)
{
if (OnMessageFired != null)
{
OnMessageFired(obj, eventArgs);
}
}
}
public class MessageEventArgs : EventArgs
{
public string ItemUri { get; private set; }
public Operation Operation { get; private set; }
public Status Status { get; private set; }
public MessageEventArgs(string itemUri, Operation operation, Status status)
{
ItemUri = itemUri;
Operation = operation;
Status = status;
}
}
public enum Operation
{
Upload,Download
}
public enum Status
{
Started,Finished
}
this class now contain the Event
, EventArgs
and EventArgs Enums
and the function
responsible for firing the event
这个类现在包含事件、EventArgs和EventArgs Enums以及负责触发事件的函数
sorry for this long answer
抱歉这么长时间的回答