2010-03-15 32 views
17

我希望創建自己的事件併發送它們。 我從來沒有在C#中完成過這項工作,只有在Flex中。我想必須有很多不同之處。如何在C中發送事件#

任何人都可以提供一個很好的例子嗎?

+1

的WinForms,WebForms的,ASP.NET MVC或WPF? – Oded 2010-03-15 15:58:31

+1

更多背景? – Grzenio 2010-03-15 16:01:37

+0

謝謝你的回覆!他們幫助我一起;) 再次感謝! – 2010-03-15 16:30:46

回答

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

我想我可以代替EventArgs的,用我自己的類? – 2010-03-15 16:19:49

+1

是的,儘管你會使用事件處理其中T是你的事件參數的類型又名.. 公共事件的EventHandler MyEvent; – Sekhat 2010-03-15 16:21:15

+1

這也是一種常見的。NET慣例,您的自定義EventArgs類從EventArgs繼承 – Sekhat 2010-03-15 16:21:57

0

查找到'delegates'.

  • 定義委託
  • 使用委託類型的字段/屬性(添加「事件」關鍵字)
  • 你現在暴露用戶可以掛接到與事件「 + = MyEventMethod;「

希望這有助於

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

相關問題