Published: 30 May 2008
By: Faisal Khan
Download Sample Code

In-Depth analysis of animation with Silverlight 2.0 Beta.

Contents [hide]

Animating Video

In this example I’ll show you how animations can be performed using a VideoBrush. This is one of the examples that attracts the users attention by completing the animation at the right time. The video will be clipped and rotated during the animation.

Open Microsoft Expression Blend. Select the Silverlight 2.0 application template from the New Project dialog box. Name the project SilverlightVideoAnimation.

Figure 1: Setting up the project

Setting up the project

Select the Grid in the Properties panel. Under the Brushes tab select Background and set the Background to LinearGradientBrush. Choose the Color key of both sides of the gradient as you prefer. I've experimented here with a Blue Color for the gradient. Create two Rectangles by selecting the Rectangle tool from the ToolBox. Set the Width of the Rectangle to 180 and Height to 200; name the Rectangle as rectangle. Set the same Width and Height for the second Rectangle too. Name the first Rectangle rectangle and second as rectangle1.

Add a MediaElement and name it whatever you like. I chose the name mElement. Set the Width and Height to 100. Set the opacity of MediaElement to 0 and the Source property to a media file that is located in your hard drive, such as a .wmv or .avi file or whatever you like. Now Fix the Fill property of the first Rectangle. Choose VideoBrush to Fill the Rectangle. Set the Source name to the name of your MediaElement. In this case, it is mElement. Add five buttons to control the media element and the storyboard.

If you read the articles of this series carefully, you should have become familiar with the RenderTransform property of the UIElement. Here I am setting the TranslateTransform object x co-ordinate to 80, which means half of both our Rectangle elements. Set the Width and Hight of the second Rectangle to be the same as the first one. In the <VideoBrush.RenderTransform> Section set the TranslateTransform object X co-ordinate to -80 to see playing half of the video in each Rectangle when in animated mode. In the Storyboard, under the <UserControl.Resources>, I've named the Storyboard as Storyboard1 and set its RepeatBehaviour to 4x. That means both the rectangles will animate four times before finishing the animation. One thing to note about the iteration place holder, which in this case is 4x, is that it specifies the number of times an animation will repeat. The number of iterations is always followed by the lower-case character x. It represents a sort of multiplication character, where 3x means 3 times, 4x means 4 times, 2x means 2 times etc. The resulting XAML generated by the Expression Blend should look like the following:

Now add some logic to see this animation in Full Screen mode. To get this job done we need to add some procedural code. Right click over Page.xaml and select Edit in Visual Studio. This is shown in the following screenshot.

Figure 2: Setting up the animation to run in full screen mode

Setting up the animation to run in full screen mode

Add a reference to System.Windows.Interop in the namespace section of the Page.xaml.cs codebehind file. This will help us in using the SilverlightHost class. The purpose of this class is to provide information on Silverlight-based application's instantiation settings and exposing some of the HTML DOM values for the hosted plug-in instance. First I’ve created a new instance of SilverlightHost object. In this case it is declared as slHost.

To specify the size I’ve created a method named SizeUI, like the following:

Here I’ve fixed the width and height of the rectangles and media element for the final output. Next I’ve defined an event handler called LayoutRoot_FullScreenChanged and called my SizeUI method to add some functionality.

To start the storyboard I’ve created a LoadAnimation method and called the storyboard’s Begin method and also the Play method of the media element to control video by clicking the play button.

To stop the animation I’ve defined a Storyboard_Stop event.

I’ve also defined two events for the resume and pause functionalities; and called the storyboard’s Resume and Pause method.

At last I’ve defined an event for full screen support which has been wired to btnFullScreen, which is a button in this case. The logic for this event is quite simple. I’ve checked the condition using an if block, by determining whether the Silverlight plug-in is running in full screen mode or not.

By clicking this button we can see the animated video in full screen.

Figure 3: Video Animation in Action

Video Animation in Action

Summary

You can surely make this application more effective as we've only touched on the basics of video animation with Silverlight. I hope you’ve enjoyed this trip into animation with Silverlight 2.0 beta and that you are ready to get your feet wet.

Thanks very much and happy coding.

About Faisal Khan

Currently working at Vectorform as a Silverlight developer. His passions revolve around creating the next generation desktop application and Rich Internet applications. His goals are to create applications which will serve the user in a friendly and traditional way, but will keep pace with next ge...

This author has published 11 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


WPF Tutorial
With all the new technology that Microsoft is releasing, it's hard to keep up. WPF is one such techn...
Silverlight "WPF/E" first steps: Getting started with simple analog clock
This article is an introductory article on how to build a WPF/E simple web application that represen...
Introduction to XAML Part 1
You are coding in .NET and have basic knowledge of XML. You heard about that Windows Presentation F...
Hello Silverlight : Start your Silverlight journey today!
Start your Silverlight development journey today with this step-by-step article.
RIA Services With Silverlight 3 - Part 1
What RIA services actually are and how to use them with Silverlight.

You might also be interested in the following related blog posts


Four Transforms read more
API changes introduced by the new CoverFlow control read more
Moonlight 1.0 Released, Silverlight script updated – and a Chrome hack read more
Silverlight MVP read more
Silverlight Controls read more
Some Silverlight ecosystem updates read more
Why Embedded Silverlight Makes Sense read more
Some Silverlight ecosystem updates read more
What I'm Thinking About April read more
Dependency Properties, Continued read more
Top
 
 
 

Please login to rate or to leave a comment.

Product Spotlight