Поделиться через


EventHandler<TEventArgs> Delegate

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents the method that will handle an event.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Delegate Sub EventHandler(Of TEventArgs As EventArgs) ( _
    sender As Object, _
    e As TEventArgs _
)
public delegate void EventHandler<TEventArgs>(
    Object sender,
    TEventArgs e
)
where TEventArgs : EventArgs

Type Parameters

  • TEventArgs
    The type of the event data generated by the event.

Parameters

  • e
    Type: TEventArgs
    An EventArgs that contains the event data.

Remarks

The event model in the .NET Framework is based on having an event delegate that connects an event with its handler. To raise an event, two elements are needed:

  • A delegate that refers to a method that provides the response to the event.

  • A class that holds the event data.

The delegate is a type that defines a signature, that is, the return value type and parameter list types for a method. You can use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.

The standard signature of an event handler delegate defines a method that does not return a value, whose first parameter is of type Object and refers to the instance that raises the event, and whose second parameter is derived from type EventArgs and holds the event data. If the event does not generate event data, the second parameter is simply an instance of EventArgs. Otherwise, the second parameter is a custom type derived from EventArgs and supplies any fields or properties needed to hold the event data.

EventHandler<TEventArgs> is a predefined delegate that represents an event handler method for an event, regardless of whether the event generates event data. If your event does not generate event data, substitute EventArgs for the generic type parameter; otherwise, supply your own custom event data type and substitute it for the generic type parameter.

The advantage of using EventHandler<TEventArgs> is that you do not need to code your own custom delegate if your event generates event data. Additionally, the .NET Framework needs only one implementation to support EventHandler<TEventArgs> regardless of the event data type you substitute for the generic type parameter.

To associate the event with the method that will handle the event, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.

For more information about event handler delegates, see Events Overview for Silverlight

Examples

The following code example declares event data and a generic EventHandler<TEventArgs> delegate that uses the event data, and shows how the event is raised.

' This example demonstrates the EventHandler(Of T) delegate.

Public Class MyEventArgs
    Inherits EventArgs
    Private msg As String

    Public Sub New(ByVal messageData As String)
        msg = messageData
    End Sub

    Public Property Message() As String
        Get
            Return msg
        End Get
        Set(ByVal value As String)
            msg = value
        End Set
    End Property
End Class

Public Class HasEvent
    ' Declare an event of delegate type EventHandler of 
    ' MyEventArgs.
    Public Event SampleEvent As EventHandler(Of MyEventArgs)

    Public Sub DemoEvent(ByVal val As String)
        RaiseEvent SampleEvent(Me, New MyEventArgs(val))
    End Sub
End Class

Public Class Example
    Private Shared outputBlock As System.Windows.Controls.TextBlock
    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

        Example.outputBlock = outputBlock
        Dim he As New HasEvent()
        AddHandler he.SampleEvent, AddressOf ExampleEventHandler
        he.DemoEvent("Hey there, Bruce!")
        he.DemoEvent("How are you today?")
        he.DemoEvent("I'm pretty good.")
        he.DemoEvent("Thanks for asking!")
    End Sub

    Private Shared Sub ExampleEventHandler(ByVal src As Object, _
                                          ByVal mea As MyEventArgs)
        outputBlock.Text &= mea.Message & vbCrLf
    End Sub
End Class
'---------------------------------------------------------
'
'This example produces the following results:
'
'Hey there, Bruce!
'How are you today?
'I'm pretty good.
'Thanks for asking!
'
// This example demonstrates the EventHandler<T> delegate.

using System;
using System.Collections.Generic;

//---------------------------------------------------------
public class MyEventArgs : EventArgs
{
   private string msg;

   public MyEventArgs(string messageData)
   {
      msg = messageData;
   }
   public string Message
   {
      get { return msg; }
      set { msg = value; }
   }
}
//---------------------------------------------------------
public class HasEvent
{
   // Declare an event of delegate type EventHandler of 
   // MyEventArgs.

   public event EventHandler<MyEventArgs> SampleEvent;

   public void DemoEvent(string val)
   {
      // Copy to a temporary variable to be thread-safe.
      EventHandler<MyEventArgs> temp = SampleEvent;
      if (temp != null)
         temp(this, new MyEventArgs(val));
   }
}
//---------------------------------------------------------
public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      Example.outputBlock = outputBlock;
      HasEvent he = new HasEvent();
      he.SampleEvent +=
                 new EventHandler<MyEventArgs>(ExampleEventHandler);
      he.DemoEvent("Hey there, Bruce!");
      he.DemoEvent("How are you today?");
      he.DemoEvent("I'm pretty good.");
      he.DemoEvent("Thanks for asking!");
   }
   private static void ExampleEventHandler(object src, MyEventArgs mea)
   {
      outputBlock.Text += mea.Message + "\n";
   }
}
//---------------------------------------------------------
/*
This example produces the following results:

Hey there, Bruce!
How are you today?
I'm pretty good.
Thanks for asking!

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.