[英]How can I make my own event in C#?
如何在 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();
}
}
}
我在我的事件文章中對事件和代表進行了完整的討論。 對於最簡單的事件,您可以聲明一個公共事件,編譯器將創建一個事件和一個字段來跟蹤訂閱者:
public event EventHandler Foo;
如果您需要更復雜的訂閱/取消訂閱邏輯,您可以明確地這樣做:
public event EventHandler Foo
{
add
{
// Subscription logic here
}
remove
{
// Unsubscription logic here
}
}
您可以使用以下代碼聲明事件:
public event EventHandler MyOwnEvent;
如果需要,可以使用自定義委托類型而不是 EventHandler。
您可以在文章事件教程(MSDN) 中找到有關使用 .NET 中的事件的詳細信息/教程。
要做到這一點,我們必須知道三個組成部分
firing the Event
的地方responding to the Event
的地方事件本身
一個。 事件
b.EventArgs
c。 EventArgs 枚舉
現在讓我們創建在調用 function 時觸發的事件
但我解決這個問題的順序是這樣的:我在創建它之前使用 class
負責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 是一個 static class 我稍后會解釋
下一步是
負責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));
第三步
我在名為 NetLog 的 class 中扭曲了事件
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
}
此 class 現在包含the Event
、 EventArgs
和EventArgs Enums
以及負責觸發事件the function
抱歉這么長的回答
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.