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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.