EventArgs Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Rappresenta la classe di base per le classi che contengono dati dell'evento e fornisce un valore da utilizzare per gli eventi che non includono i dati dell'evento.
public ref class EventArgs
public class EventArgs
[System.Serializable]
public class EventArgs
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class EventArgs
type EventArgs = class
[<System.Serializable>]
type EventArgs = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type EventArgs = class
Public Class EventArgs
- Ereditarietà
-
EventArgs
- Derivato
- Attributi
Esempio
Nell'esempio seguente viene illustrata una classe di dati evento personalizzata denominata ThresholdReachedEventArgs che deriva dalla EventArgs classe . Un'istanza della classe di dati dell'evento viene passata al gestore eventi per l'evento ThresholdReached .
using System;
namespace ConsoleApplication3
{
public class Program3
{
public static void Main()
{
Counter c = new(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 readonly int _threshold;
private int _total;
public Counter(int passedThreshold)
{
_threshold = passedThreshold;
}
public void Add(int x)
{
_total += x;
if (_total >= _threshold)
{
ThresholdReachedEventArgs args = new()
{
Threshold = _threshold,
TimeReached = DateTime.Now
};
OnThresholdReached(args);
}
}
protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{
ThresholdReached?.Invoke(this, e);
}
public event EventHandler<ThresholdReachedEventArgs>? ThresholdReached;
}
public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
}
open System
type ThresholdReachedEventArgs(threshold, timeReached) =
inherit EventArgs()
member _.Threshold = threshold
member _.TimeReached = timeReached
type Counter(threshold) =
let mutable total = 0
let thresholdReached = Event<_>()
member this.Add(x) =
total <- total + x
if total >= threshold then
let args = ThresholdReachedEventArgs(threshold, DateTime.Now)
thresholdReached.Trigger(this, args)
[<CLIEvent>]
member _.ThresholdReached = thresholdReached.Publish
let c_ThresholdReached(sender, e: ThresholdReachedEventArgs) =
printfn $"The threshold of {e.Threshold} was reached at {e.TimeReached}."
exit 0
let c = Counter(Random().Next 10)
c.ThresholdReached.Add c_ThresholdReached
printfn "press 'a' key to increase total"
while Console.ReadKey(true).KeyChar = 'a' do
printfn "adding one"
c.Add 1
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
Commenti
Questa classe funge da classe base per tutte le classi che rappresentano i dati dell'evento. Ad esempio, la System.AssemblyLoadEventArgs classe deriva da EventArgs e viene usata per contenere i dati per gli eventi di caricamento degli assembly. Per creare una classe di dati evento personalizzata, creare una classe che deriva dalla EventArgs classe e fornire le proprietà per archiviare i dati necessari. Il nome della classe di dati dell'evento personalizzata deve terminare con EventArgs.
Per passare un oggetto che non contiene dati, utilizzare il Empty campo .
Per altre informazioni sugli eventi, vedere l'articolo Gestione e generazione di eventi .
Costruttori
| Nome | Descrizione |
|---|---|
| EventArgs() |
Inizializza una nuova istanza della classe EventArgs. |
Campi
| Nome | Descrizione |
|---|---|
| Empty |
Fornisce un valore da utilizzare con gli eventi che non dispongono di dati dell'evento. |
Metodi
| Nome | Descrizione |
|---|---|
| Equals(Object) |
Determina se l'oggetto specificato è uguale all'oggetto corrente. (Ereditato da Object) |
| GetHashCode() |
Funge da funzione hash predefinita. (Ereditato da Object) |
| GetType() |
Ottiene il Type dell'istanza corrente. (Ereditato da Object) |
| MemberwiseClone() |
Crea una copia superficiale del Objectcorrente. (Ereditato da Object) |
| ToString() |
Restituisce una stringa che rappresenta l'oggetto corrente. (Ereditato da Object) |