EventHandler Temsilci
Tanım
Olay verisi olmayan bir olayı işleyecek yöntemi temsil eder.Represents the method that will handle an event that has no event data.
public delegate void EventHandler(System::Object ^ sender, EventArgs ^ e);
public delegate void EventHandler(object sender, EventArgs e);
public delegate void EventHandler(object? sender, EventArgs e);
[System.Serializable]
public delegate void EventHandler(object sender, EventArgs e);
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void EventHandler(object sender, EventArgs e);
type EventHandler = delegate of obj * EventArgs -> unit
[<System.Serializable>]
type EventHandler = delegate of obj * EventArgs -> unit
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type EventHandler = delegate of obj * EventArgs -> unit
Public Delegate Sub EventHandler(sender As Object, e As EventArgs)
Parametreler
- sender
- Object
Olayın kaynağı.The source of the event.
Olay verisi içermeyen bir nesne.An object that contains no event data.
- Devralma
- Öznitelikler
Örnekler
Aşağıdaki örnek, ThresholdReached
bir temsilciyle ilişkili adlı bir olay gösterir EventHandler .The following example shows an event named ThresholdReached
that is associated with an EventHandler delegate. Temsilciye atanan yöntemi EventHandler OnThresholdReached
yönteminde çağrılır.The method assigned to the EventHandler delegate is called in the OnThresholdReached
method.
using namespace System;
public ref class ThresholdReachedEventArgs : public EventArgs
{
public:
property int Threshold;
property DateTime TimeReached;
};
public ref class Counter
{
private:
int threshold;
int total;
public:
Counter() {};
Counter(int passedThreshold)
{
threshold = passedThreshold;
}
void Add(int x)
{
total += x;
if (total >= threshold) {
ThresholdReachedEventArgs^ args = gcnew ThresholdReachedEventArgs();
args->Threshold = threshold;
args->TimeReached = DateTime::Now;
OnThresholdReached(args);
}
}
event EventHandler<ThresholdReachedEventArgs^>^ ThresholdReached;
protected:
virtual void OnThresholdReached(ThresholdReachedEventArgs^ e)
{
ThresholdReached(this, e);
}
};
public ref class SampleHandler
{
public:
static void c_ThresholdReached(Object^ sender, ThresholdReachedEventArgs^ e)
{
Console::WriteLine("The threshold of {0} was reached at {1}.",
e->Threshold, e->TimeReached);
Environment::Exit(0);
}
};
void main()
{
Counter^ c = gcnew Counter((gcnew Random())->Next(10));
c->ThresholdReached += gcnew EventHandler<ThresholdReachedEventArgs^>(SampleHandler::c_ThresholdReached);
Console::WriteLine("press 'a' key to increase total");
while (Console::ReadKey(true).KeyChar == 'a') {
Console::WriteLine("adding one");
c->Add(1);
}
}
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Counter c = new Counter(new Random().Next(10));
c.ThresholdReached += c_ThresholdReached;
Console.WriteLine("press 'a' key to increase total");
while (Console.ReadKey(true).KeyChar == 'a')
{
Console.WriteLine("adding one");
c.Add(1);
}
}
static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
{
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
Environment.Exit(0);
}
}
class Counter
{
private int threshold;
private int total;
public Counter(int passedThreshold)
{
threshold = passedThreshold;
}
public void Add(int x)
{
total += x;
if (total >= threshold)
{
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Threshold = threshold;
args.TimeReached = DateTime.Now;
OnThresholdReached(args);
}
}
protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{
EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
}
public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
}
Module Module1
Sub Main()
Dim c As Counter = New Counter(New Random().Next(10))
AddHandler c.ThresholdReached, AddressOf c_ThresholdReached
Console.WriteLine("press 'a' key to increase total")
While Console.ReadKey(True).KeyChar = "a"
Console.WriteLine("adding one")
c.Add(1)
End While
End Sub
Sub c_ThresholdReached(sender As Object, e As ThresholdReachedEventArgs)
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached)
Environment.Exit(0)
End Sub
End Module
Class Counter
Private threshold As Integer
Private total As Integer
Public Sub New(passedThreshold As Integer)
threshold = passedThreshold
End Sub
Public Sub Add(x As Integer)
total = total + x
If (total >= threshold) Then
Dim args As ThresholdReachedEventArgs = New ThresholdReachedEventArgs()
args.Threshold = threshold
args.TimeReached = DateTime.Now
OnThresholdReached(args)
End If
End Sub
Protected Overridable Sub OnThresholdReached(e As ThresholdReachedEventArgs)
RaiseEvent ThresholdReached(Me, e)
End Sub
Public Event ThresholdReached As EventHandler(Of ThresholdReachedEventArgs)
End Class
Class ThresholdReachedEventArgs
Inherits EventArgs
Public Property Threshold As Integer
Public Property TimeReached As DateTime
End Class
Açıklamalar
.NET Framework olay modeli, bir olayı işleyicisiyle bağlayan bir olay temsilcisine sahip olmaya dayalıdır.The event model in the .NET Framework is based on having an event delegate that connects an event with its handler. Bir olayı yükseltmek için iki öğe gereklidir:To raise an event, two elements are needed:
Olaya yanıt sağlayan yöntemini tanımlayan bir temsilci.A delegate that identifies the method that provides the response to the event.
İsteğe bağlı olarak, olay verileri sağlıyorsa olay verilerini tutan bir sınıf.Optionally, a class that holds the event data, if the event provides data.
Temsilci bir imzayı tanımlayan bir türdür, diğer bir deyişle, bir yöntemi için dönüş değeri türü ve parametre listesi türleri.The delegate is a type that defines a signature, that is, the return value type and parameter list types for a method. Temsilci türünü, temsilciyle aynı imzaya sahip herhangi bir yönteme başvurabilen bir değişken bildirmek için kullanabilirsiniz.You can use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.
Bir olay işleyicisi temsilcisinin standart imzası, değer döndürmeyen bir yöntemi tanımlar.The standard signature of an event handler delegate defines a method that does not return a value. Bu yöntemin ilk parametresi türündedir Object ve olayı başlatan örneğe başvurur.This method's first parameter is of type Object and refers to the instance that raises the event. İkinci parametresi türünden türetilir EventArgs ve olay verilerini tutar.Its second parameter is derived from type EventArgs and holds the event data. Olay olay verileri oluşturmadığından, ikinci parametre yalnızca EventArgs.Empty alanın değeridir.If the event does not generate event data, the second parameter is simply the value of the EventArgs.Empty field. Aksi takdirde, ikinci parametre öğesinden türetilmiş bir türdür EventArgs ve olay verilerini tutmak için gereken tüm alanları veya özellikleri sağlar.Otherwise, the second parameter is a type derived from EventArgs and supplies any fields or properties needed to hold the event data.
EventHandlerTemsilci, veri üretmayan bir olay için bir olay işleyicisi yöntemini temsil eden önceden tanımlanmış bir temsilcidir.The EventHandler delegate is a predefined delegate that specifically represents an event handler method for an event that does not generate data. Olaylarınız veri üretürse, genel temsilci sınıfını kullanmanız gerekir EventHandler<TEventArgs> .If your event does generate data, you must use the generic EventHandler<TEventArgs> delegate class.
Olayı, olayı işleyecek yöntemle ilişkilendirmek için, bir temsilci örneğini olaya ekleyin.To associate the event with the method that will handle the event, add an instance of the delegate to the event. Olay her gerçekleştiğinde, olay işleyici (siz temsilciyi kaldırmadığınız sürece) çağrılır.The event handler is called whenever the event occurs, unless you remove the delegate.
Olay işleyicisi temsilcileri hakkında daha fazla bilgi için bkz. olayları işleme veoluşturma.For more information about event handler delegates, see Handling and Raising Events.
Uzantı Metotları
GetMethodInfo(Delegate) |
Belirtilen temsilci tarafından temsil edilen yöntemi temsil eden bir nesne alır.Gets an object that represents the method represented by the specified delegate. |
Şunlara uygulanır
Ayrıca bkz.
- EventHandler<TEventArgs>
- EventArgs
- Delegate
- Olaylar Oluşturma ve İşlemeHandling and Raising Events
- Nasıl yapılır: Olaylar Oluşturma ve KullanmaHow to: Raise and Consume Events
- Olaylar (Visual Basic)Events (Visual Basic)
- Olaylar (C# Programlama Kılavuzu)Events (C# Programming Guide)
- Olaylar ve yönlendirilmiş olaylara genel bakış (Windows Mağazası uygulamaları)Events and routed events overview (Windows store apps)