EventArgs (Clase)

EventArgs es la clase base para las clases que contienen datos de eventos.

Espacio de nombres: System
Ensamblado: mscorlib (en mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class EventArgs
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class EventArgs
SerializableAttribute 
ComVisibleAttribute(true) 
public class EventArgs

Esta clase no contiene datos de eventos; utilizan esta clase los eventos que no pasan información de estado a un controlador de eventos cuando se produce un evento. Si el controlador de eventos requiere información de estado, la aplicación debe derivar una clase a partir de ésta para incluir los datos.

Por ejemplo, la clase System.AssemblyLoadEventArgs se utiliza para incluir los datos de los eventos de carga de ensamblados y contiene un System.Reflection.Assembly que describe el ensamblado cargado.

Para obtener más información sobre eventos, vea el tema EventHandler.

En el siguiente ejemplo de código se muestra el uso de EventArgs.

En este ejemplo, FireEventArgs es un conjunto de argumentos de evento derivado de EventArgs que se ha pasado a FireEventHandler cuando se ha provocado un evento al llamar a ActivateFireAlarm.


using System;

// FireEventArgs: a custom event inherited from EventArgs.

public class FireEventArgs: EventArgs {
	public FireEventArgs(string room, int ferocity) {
		this.room = room;
		this.ferocity = ferocity;
	}

	// The fire event will have two pieces of information-- 
	// 1) Where the fire is, and 2) how "ferocious" it is.  

	public string room;
	public int ferocity;

}	//end of class FireEventArgs


// Class with a function that creates the eventargs and initiates the event
public class FireAlarm {

	// Events are handled with delegates, so we must establish a FireEventHandler
	// as a delegate:

	public delegate void FireEventHandler(object sender, FireEventArgs fe);

	// Now, create a public event "FireEvent" whose type is our FireEventHandler delegate. 

	public event FireEventHandler FireEvent;	

	// This will be the starting point of our event-- it will create FireEventArgs,
	// and then raise the event, passing FireEventArgs. 

	public void ActivateFireAlarm(string room, int ferocity) {

		FireEventArgs fireArgs = new FireEventArgs(room, ferocity);

		// Now, raise the event by invoking the delegate. Pass in 
		// the object that initated the event (this) as well as FireEventArgs. 
		// The call must match the signature of FireEventHandler.

		FireEvent(this, fireArgs); 
	}
}	// end of class FireAlarm


// Class which handles the event

class FireHandlerClass {

	// Create a FireAlarm to handle and raise the fire events. 

	public FireHandlerClass(FireAlarm fireAlarm)	{

		// Add a delegate containing the ExtinguishFire function to the class'
		// event so that when FireAlarm is raised, it will subsequently execute 
		// ExtinguishFire.

		fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);
	}

	// This is the function to be executed when a fire event is raised. 
 
	void ExtinguishFire(object sender, FireEventArgs fe) {

		Console.WriteLine("\nThe ExtinguishFire function was called by {0}.", sender.ToString());

		// Now, act in response to the event.

		if (fe.ferocity < 2)
			Console.WriteLine("This fire in the {0} is no problem.  I'm going to pour some water on it.", fe.room);
		else if (fe.ferocity < 5)
			Console.WriteLine("I'm using FireExtinguisher to put out the fire in the {0}.",  fe.room);
		else 
			Console.WriteLine("The fire in the {0} is out of control.  I'm calling the fire department!", fe.room);
	}
}	//end of class FireHandlerClass

public class FireEventTest {
	public static void Main () 	{	

		// Create an instance of the class that will be firing an event.

		FireAlarm myFireAlarm = new FireAlarm();
		
		// Create an instance of the class that will be handling the event. Note that 
		// it receives the class that will fire the event as a parameter. 

		FireHandlerClass myFireHandler = new FireHandlerClass(myFireAlarm);
		
		//use our class to raise a few events and watch them get handled
		myFireAlarm.ActivateFireAlarm("Kitchen", 3);
		myFireAlarm.ActivateFireAlarm("Study", 1);
		myFireAlarm.ActivateFireAlarm("Porch", 5);
		
		return;

	}	//end of main

}	// end of FireEventTest


Los miembros estáticos públicos (Shared en Visual Basic) de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition

.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.

.NET Framework

Compatible con: 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 2.0, 1.0

Adiciones de comunidad

AGREGAR
Mostrar: