How do event handlers work




















An event handler is an implementation of the EventHandler interface. The handle method of this interface provides the code that is executed when the event that is associated with the handler is received by the node that registered the handler.

To register a handler, use the addEventHandler method. This method takes the event type and the handler as arguments. In Example , the first handler is added to a single node and processes a specific event type.

A second handler for handling input events is defined and registered by two different nodes. The same handler is also registered for two different types of events. Note that an event handler that is defined for one type of event can also be used for any subtypes of that event. See Event Types for information on the hierarchy of event types. When you no longer want an event handler to process events for a node or for an event type, remove the handler using the removeEventHandler method.

In Example , the handler defined in Example is removed from the DragEvent. The handler is still executed by myNode2 and by myNode1 for the MouseEvent.

To remove an event handler that was registered by a convenience method, pass null to the convenience method, for example, node1. Event handlers are typically used on a the leaf nodes or on a branch node of the event dispatch chain and are called during the event bubbling phase of event handling. Use a handler on a branch node to perform actions such as defining a default response for all child nodes.

To see an example of how handlers can be used, download the KeyboardExample. The following sections describe the handlers that are used by this example. They inflate the markup and make it less readable and harder to debug. For more information see Inline event handlers. By convention, Javascript objects that fire events have a corresponding "onevent" properties named by prefixing "on" to the name of the event.

These properties are called to run associated handler code when the event is fired, and may also be called directly by your own code. To set event handler code you can just assign it to the appropriate onevent property. Only one event handler can be assigned for every event in an element. If needed the handler can be replaced by assigning another function to the same property. Below we show how to set a simple greet function for the click event using the onclick property.

Note that an object representing the event is passed as the first argument to the event handler. This event object either implements or is derived from the Event interface. NET follows a naming pattern of ending all event data classes with EventArgs. You determine which event data class is associated with an event by looking at the delegate for the event.

The EventArgs class is the base type for all event data classes. EventArgs is also the class you use when an event does not have any data associated with it. When you create an event that is only meant to notify other classes that something happened and does not need to pass any data, include the EventArgs class as the second parameter in the delegate. You can pass the EventArgs. Empty value when no data is provided. The EventHandler delegate includes the EventArgs class as a parameter.

When you want to create a customized event data class, create a class that derives from EventArgs , and then provide any members needed to pass data that is related to the event. Typically, you should use the same naming pattern as. NET and end your event data class name with EventArgs. The following example shows an event data class named ThresholdReachedEventArgs. It contains properties that are specific to the event being raised. To respond to an event, you define an event handler method in the event receiver.

This method must match the signature of the delegate for the event you are handling. In the event handler, you perform the actions that are required when the event is raised, such as collecting user input after the user clicks a button.

To receive notifications when the event occurs, your event handler method must subscribe to the event. The method subscribes to the ThresholdReached event. NET allows subscribers to register for event notifications either statically or dynamically.

Static event handlers are in effect for the entire life of the class whose events they handle. Dynamic event handlers are explicitly activated and deactivated during program execution, usually in response to some conditional program logic. For example, they can be used if event notifications are needed only under certain conditions or if an application provides multiple event handlers and run-time conditions define the appropriate one to use.

The example in the previous section shows how to dynamically add an event handler. If your class raises multiple events, the compiler generates one field per event delegate instance. If the number of events is large, the storage cost of one field per delegate may not be acceptable. For those situations,. NET provides event properties that you can use with another data structure of your choice to store event delegates.

Event properties consist of event declarations accompanied by event accessors.



0コメント

  • 1000 / 1000