In the previous article I described what are the different Layout Management Panels in Silverlight and how and when they are used. In this article I will try to describe how to add/manage events in Silverlight.
What is an Event
An event is a message sent by an object to signal the occurrence of an action. The action could be caused by user interaction, such as a mouse click, or it could be triggered by the internal logic of a class. The object that raises the event is called the Event Sender. The object that captures the event and responds to it is called the Event Receiver.
Event handling is performed in two event cases
- Input events
- Non-input events.
Input Events: The hosting browser for Silverlight plug-in handles the input events input stimulus, as Silverlight works within the hosting browser plug-in architecture. The event is sent to Silverlight plug-in through the browser. Later the event is raised in the Silverlight Object Model.
Non-Input Events: These events report a change state of a particular object. For instance, progress of initiated actions by the web client. Certain non-input events provide information of objects as a framework level for lifetime. An example of such event is FreameworkElementLoaded event.
Silverlight events are essentially the same event concept as defined by the common language runtime (CLR) and the .NET Framework. Similar to WPF, you can assign handlers for events as part of the declarations for UI elements in XAML, or you can use language-specific syntax to add the handlers in code. In order to associate a class with XAML file you need to specify the x: Class attribute in its root element.
Adding Event Handlers in XAML
A typical event handler declaration in XAML might look like this.
<Button Name="showUpdatesButton" Click="Button_Click"/>
The actual handler is written in one of the programming languages that are supported by the .NET Framework for Silverlight, such as Visual Basic or C#. Below is in C#
private void Button_Click(object sender, RoutedEventArgs e)
Button b = e.OriginalSource as Button;
//more logic here
Adding Event Handlers in Managed Code
If you are using code to add event handlers to objects that appear in the run-time UI, a common practice for Silverlight is to add such handlers in response to an object lifetime event or callback, such as Loaded or OnApplyTemplate, so that the event handlers on the relevant object are ready for user-initiated events at run time. The following example illustrates first the XAML outline to give some idea of the structure and then provides the C# language syntax.
<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">
<TextBlock Name="textBlock1">Put the mouse over this text</TextBlock>
void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
textBlock1.MouseEnter += new MouseEventHandler(textBlocks_MouseEnter);
textBlock1.MouseLeave += new MouseEventHandler(textBlocks_MouseLeave);
Removing Event Handlers
In some circumstances, you might want to remove event handlers during the application lifetime. To remove event handlers, you use the CLR-language-specific syntax. In C#, you use the -= operator. In Visual Basic, you use the RemoveHandler function.
textBlock1.MouseEnter -= textBlocks_MouseEnter;
Silverlight enforces that certain operations are only permitted in the context of a handler that handles a user-initiated event. Silverlight user-initiated events include the mouse events (such as MouseLeftButtonDown), and the keyboard events (such as KeyDown). The following is a list of such operations:
- Setting IsFullScreen.
- Showing certain dialogs. This includes SaveFileDialog, OpenFileDialog, and the print dialog displayed by PrintDocument.Print.
- Navigating from a HyperlinkButton.
- Accessing the primary Clipboard API.