Chapter 6. Handling User Interaction and Events
The declarative means of XAML provide quite a number of possibilities, including creating all kinds of shapes (see Chapter 5), animating elements (see Chapter 7), and playing audio and video data (see Chapter 8). However, you can unleash the real power of Silverlight only if you add a bit of code into the mix. With previous Silverlight versions, JavaScript itself proved to be a powerful tool, since Silverlight exposes a JavaScript API to developers. Part III of this book, especially Chapter 12, will cover JavaScript access in greater detail.
Silverlight 2 offers a second way to implement dynamic effects for an application: you can use a number of .NET languages, including C#, to provide code for a Silverlight application. Although this part of the book focuses on declarative XAML, we cannot do without a certain amount of code. This book prefers C# (since the market share of that language seems to be the highest among all .NET languages), but porting the examples to Visual Basic or other languages is trivial.
This chapter explains Silverlight event handling—specifically, what events are and how to intercept and process them. Most of the event handling code will be found in the XAML code-behind file (the Filename.xaml.cs file).
Most Silverlight code examples consist of at least three files—an HTML file, an XAML file, and a C# file. As long as there are no big surprises in the HTML file, I will not reproduce it here. However, in the code ...
Get Essential Silverlight 2 Up-to-Date 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.