Chapter 5. Events and Commands

Events

An event is a programming construct that reacts to a change in state, notifying any endpoints that have registered for notification. They are ubiquitous in .NET generally, and this continues in WPF and Silverlight. Primarily, events are used to inform of user input via the mouse and keyboard, but their utility is not limited to that. Whenever a state change is detected, perhaps when an object has been loaded or initialized, an event can be fired to alert any interested third parties.

Events in .NET

In .NET, events are first-class citizens: they are represented by delegates and declared with the event keyword (see Listing 5-1).

Example 5.1. Declaring a Plain Event in .NET

delegate void MyEventHandler(); ... public ...

Get Pro WPF and Silverlight MVVM: Effective Application Development with Model-View-ViewModel now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.