Wahlin on .NET

Silverlight XAML Primer 12: Using Linear Keyframe Animations

Dan shows you how you can get more control over animations using keyframes in Silverlight apps.

Animations are an important part of Silverlight that can be used to provide an enhanced level of interactivity and "richness" within an application. By using animations, you can change the shape of a button as a user moves their mouse over it, fly images in and out of a canvas and perform many other interesting effects.

In a previous article, I discussed the fundamentals of animations and demonstrated how they can be defined in XAML using tags such as StoryBoard and DoubleAnimation. In this article, I'll demonstrate how you can gain more control over animations by using DoubleAnimationUsingKeyFrames and LinearDoubleKeyFrame XAML elements.

Introduction to Keyframe Animations
A keyframe represents a "frame" in the overall animation process. If you've ever seen how a cartoon is made, then you can think of each individual picture in the cartoon as a keyframe.

The concept of keyframes isn't new by any means. In fact, keyframes are used in many types of applications such as Adobe Flash, video editing software, Windows Presentation Foundation (WPF) and Silverlight to name, just a few.

Standard Silverlight animations typically use a XAML element named DoubleAnimation to define a target property and modify it over a specified time span. For example, you might target a TextBlock's Canvas.Left property to animate it from left to right for 3 seconds.

However, what if you'd like the TextBlock to move slowly for the first two seconds and then speed up for the remaining amount of time? This type of fine-grained control over an animation can be done using keyframes.

Several different types of keyframe animations are available in Silverlight 1.0, as shown in Table 1.

Listing 1 provides an example of how the DoubleAnimationUsingKeyFrames element can be used in Silverlight canvas to animate a TextBlock object. The animation is played automatically once the Canvas object has loaded.

The code targets a TextBlock object's Canvas.Left property and animates over a span of three seconds. The animation is repeated as long as the application is running (RepeatBehavior="Forever") and automatically reverses itself without any additional XAML code (AutoReverse="True").

Within the DoubleAnimationUsingKeyFrames element, you'll see two LinearDoubleKeyFrame elements. Each of these elements defines the value that the Canvas.Left property should be at a specified time. The first LinearDoubleKeyFrame element moves the TextBlock 50 pixels to the right over a span of two seconds. The second element moves the TextBlock 150 more pixels to the right over a span of one second. Putting the keyframes together results in the TextBlock moving slowly at the start and quickly at the end.

This is obviously a very simple example of using keyframes in a Silverlight application. Complex animations can be difficult to create using only XAML and a text editor. Fortunately, Microsoft's Expression Blend 2 product can be used to create keyframe animations visually without writing any XAML code at all. In a future article, I'll walk through the process of using Expression Blend 2 to create keyframe animations.

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube