Silverlight for Developers (Part III)
In the Last Post
Previously, I've spoken about about Visual Studio and Expression Blend, styles and templates, and some of the important foundations of XAML-based databinding. In this post, learn about customizing your applications by writing reusable code components that can be linked in declaratively.
Making It Do Your Bidding
To really make your Silverlight application look good, you're going to need to resort to custom code sometimes. The instinctive thing to do is to wire up event handlers to run at various times to create objects, adjust bounded values, or to start animations. As much as possible though, you should try to avoid this. There is a great setup for creating the necessary components that can then be referenced declaratively.
For example, suppose you want to bind to a Boolean value, but you don't want to show true/false - instead you want to show on/off. You might be tempted to forgo binding to do this manually, but this would be a mistake. The IValueConverter interface makes it trivial to accept a value and convert it to something else. You can even implement a conversion in the other direction if you need it to work in two-way databinding. Once you create the class, you will actually use it completely declaratively, and hopefully, in a completely reusable way.
As another example, suppose you want to show a message box at some point in your application. You'd probably be tempted to use the Clicked event of the button to show the message in an event handler. Instead, create an instance of the Action class. An Action object just exposes an atomic action that doesn't need to return anything - in this case, it just shows the message box when invoked. Great for simple actions that are reusable. Once created, you can drag them onto elements in your application, then choose the trigger that should start them (click, close, etc)
Speaking of triggers, if you want to trigger based on something not defined in Silverlight, you can do that too by creating your own Trigger class. You can monitor anything - events on elements, messaging queues, system activity - whatever you need. When you detect your event, you do whatever you want to do (log it maybe?) and invoke any bound activities/actions.
Finally, there will be times that you want to create actions that affect elements, such as cool bouncing effects, or a realistic drifting motion. Such actions are called Behaviors. Wire up what should happen when an object is attached or detached to your behavior, then what to do when you are triggered. It all happens declaratively and encourages maximum reuse.
Summing it Up
The world of XAML is a great place to be right now. Much of this post applies to both WPF and Silverlight. For games, utilities, or line of business applications, you can do a lot with much less code possible than Windows Forms. The graphic effects are amazing and so simple to take advantage of. Don't avoid it because you heard it was just like Flash except by Microsoft. It's much more, and being able to work completely from managed code is definitely the icing on the cake!
Resource: Get started by taking advantage of the tutorial videos available by Microsoft.