17
A
回答
43
有一種模式可用於所有庫類。它也推薦給你自己的類,特別是框架/庫代碼。但是當你偏離或跳過幾個步驟時,沒有人會阻止你。
以下是基於最簡單事件代表System.Eventhandler
的原理圖。
// The delegate type. This one is already defined in the library, in the System namespace
// the `void (object, EventArgs)` signature is also the recommended pattern
public delegate void Eventhandler(object sender, Eventargs args);
// your publishing class
class Foo
{
public event EventHandler Changed; // the Event
protected virtual void OnChanged() // the Trigger method, called to raise the event
{
// make a copy to be more thread-safe
EventHandler handler = Changed;
if (handler != null)
{
// invoke the subscribed event-handler(s)
handler(this, EventArgs.Empty);
}
}
// an example of raising the event
void SomeMethod()
{
if (...) // on some condition
OnChanged(); // raise the event
}
}
以及如何使用它:
// your subscribing class
class Bar
{
public Bar()
{
Foo f = new Foo();
f.Changed += Foo_Changed; // Subscribe, using the short notation
}
// the handler must conform to the signature
void Foo_Changed(object sender, EventArgs args) // the Handler (reacts)
{
// the things Bar has to do when Foo changes
}
}
當你擁有了信息傳遞下去:
class MyEventArgs : EventArgs // guideline: derive from EventArgs
{
public string Info { get; set; }
}
class Foo
{
public event EventHandler<MyEventArgs> Changed; // the Event
...
protected virtual void OnChanged(string info) // the Trigger
{
EventHandler handler = Changed; // make a copy to be more thread-safe
if (handler != null)
{
var args = new MyEventArgs(){Info = info}; // this part will vary
handler(this, args);
}
}
}
class Bar
{
void Foo_Changed(object sender, MyEventArgs args) // the Handler
{
string s = args.Info;
...
}
}
更新
protected virtual void OnChanged(string info) // the Trigger
{
var args = new MyEventArgs{Info = info}; // this part will vary
Changed?.Invoke(this, args);
}
0
3
在C#中使用的代表活動。
public static event EventHandler<EventArgs> myEvent;
static void Main()
{
//add method to be called
myEvent += Handler;
//call all methods that have been added to the event
myEvent(this, EventArgs.Empty);
}
static void Handler(object sender, EventArgs args)
{
Console.WriteLine("Event Handled!");
}
3
:與C#6開始的「觸發」方法調用代碼變得輕鬆了許多,空試驗可以用空條件運算?.
同時保持線程安全,縮短不進行復制使用典型的.NET事件模式,並假設在事件中不需要任何特殊參數。你的典型事件和調度模式看起來像這樣。
public class MyClassWithEvents
{
public event EventHandler MyEvent;
protected void OnMyEvent(object sender, EventArgs eventArgs)
{
if (MyEvent != null)
{
MyEvent(sender, eventArgs);
}
}
public void TriggerMyEvent()
{
OnMyEvent(sender, eventArgs);
}
}
東西綁到事件可以是簡單的:
public class Program
{
public static void Main(string[] args)
{
MyClassWithEvents obj = new MyClassWithEvents();
obj.MyEvent += obj_myEvent;
}
private static void obj_myEvent(object sender, EventArgs e)
{
//Code called when my event is dispatched.
}
}
的鏈接看看上this MSDN page
+0
並感謝您的幫助! – 2010-03-15 16:31:35
相關問題
- 1. 如何在PyQt中發送UI事件?
- 2. 如何在C#中的timerTick事件中發送參數(參數)?
- 3. 如何在JNI中將事件/信號從C發送到Java
- 4. 如何發送UIEventTypeRemoteControl事件?
- 5. 在ASP.NET C#中自動引發事件發送電子郵件
- 6. 如何在VueJS發送後發送事件?
- 7. 如何在貓鼬中間件中發送事件?
- 8. 我如何得到郵件通過SmtpClient C#發送的事件
- 9. 如何在BroadcastReceiver事件上發送HTTP POST請求OnReceive事件
- 10. 在C中轉發事件#
- 11. 如何發送c#中的RFC822文件#
- 12. 如何發送郵件在Asp.net用C#
- 13. 服務器發送的事件:如何觸發發送
- 14. 在X11發送KeyPress事件
- 15. 發送事件從SWIFT或Objective-C的
- 16. C# - 發送值的Click事件
- 17. 解決。 cmsmadesimple事件如何發送?
- 18. AS3:如何從actionscript類發送事件
- 19. 如何使用onclick事件發送值?
- 20. 如何給火花發送事件GridColumn
- 21. 如何發送按鈕事件 - UIControlEventTouchUpInside
- 22. 如何通過C#將事件發送到Windows事件收集器 - WSMan
- 23. c#如何發送帶有事件的數組?
- 24. 如何發送一個c#事件到工作lua腳本?
- 25. 如何使用套接字c/C++在塊中發送文件?
- 26. 如何在Objective-C中發送和接收數據以及事件?
- 27. 如何將鼠標事件發送到發送前的ajaxSetup?
- 28. 如何在事件發生時將事件發送給另一個班級?
- 29. 如何在C++中激發c#COM事件?
- 30. 在Python中處理Outlook發送事件
的WinForms,WebForms的,ASP.NET MVC或WPF? – Oded 2010-03-15 15:58:31
更多背景? – Grzenio 2010-03-15 16:01:37
謝謝你的回覆!他們幫助我一起;) 再次感謝! – 2010-03-15 16:30:46