Book Image

Instant Silverlight 5 Animation

By : Nick Polyak
Book Image

Instant Silverlight 5 Animation

By: Nick Polyak

Overview of this book

Silverlight is a Web technology that allows you to create both large business applications and little ads for your web page. Silverlight's main advantage is the ability to create rich UIs easily. In this book we will show how to build animations for different types of Silverlight applications in order to create great user experience."Instant Silverlight 5 Animation" is a practical guide to creating great user experiences in Silverlight. This book will clear Silverlight/WPF concepts needed for creating animations as well as practical examples of creating animations that will help you become an efficient developer for creating animations with Silverlight.This book provides a number of hands on examples of creating Silverlight animations in order to improve the user experience whether you are building a Silverlight Business application or a Silverlight banner ad. We also talk about ways to integrate Silverlight animations with business logic quickly and in the least invasive way. This book can be of help to both beginners and advanced developers. It starts talking about Silverlight concepts like dependency/attached properties and bindings. Then it goes into nitty-gritty detail of creating different animations for different application types. We explain how to animate custom controls, page navigation, how to imitate animation of random processes like fire or moving clouds. We talk about creating 3-D animations and building banner ads for your web page. Every concept, we describe in this book, is supported by small, detailed and easy to understand samples.
Table of Contents (16 chapters)

Attached properties


One constraint on the dependency properties is that they have to be defined within a class that uses them. In many cases, however, developers might want to add properties to an object of a predefined class without extending the class. WPF and Silverlight came up with a new concept that allows doing just that – attached properties (APs). APs can be defined in some (usually static) class and can be used to attach properties to any object derived from a DependencyObject.

An attached property sample can be found in the SpinningWithAttachedPropertySample folder. To create your own sample, create a new project and add a C# file/class to it called AttachedProperties. Make this class static and use the propa snippet to create the RotateAngle attached property in it:

#region RotationAngle attached Property
public static double GetRotationAngle(DependencyObject obj)
{
    return (double)obj.GetValue(RotationAngleProperty);
}

public static void SetRotationAngle(DependencyObject obj, double value)
    {
    obj.SetValue(RotationAngleProperty, value);
}

public static readonly DependencyProperty RotationAngleProperty =
DependencyProperty.RegisterAttached
(
    "RotationAngle",
    typeof(double),
    typeof(AttachedProperties),
    new PropertyMetadata(0.0)
);
#endregion RotationAngle attached Property

You can see that unlike dependency properties, the attached properties have two static accessor methods GetRotationAngle and SetRotationAngle.

Now we can animate this attached property within the MainPage.xaml file in a very similar way to animating the dependency property. In the following section, we show the regions of XAML code that are different from the dependency property code.

In our attached property animation project, we will define a Storyboard object in exactly the same way as we did for the dependency property, the only difference is that we cannot specify Storyboard.TargetProperty within XAML:

<UserControl.Resources>
        <Storyboard x:Key="RotationStoryboard"
                    Storyboard.TargetName="TheRotatingRectangle">
            <DoubleAnimation BeginTime="00:00:00"
                             Duration="00:00:01"
                             From="0"
                             To="360"
                             RepeatBehavior="Forever" />
        </Storyboard>
</UserControl.Resources>

Unfortunately, Silverlight does not allow a storyboard to reference a custom attached property in XAML. Due to this limitation, we are forced to add such a reference in the C# code-behind.

The following is the XAML definition of a spinning Rectangle. The only difference between this code and the DP-related code previously presented is that we are using the full path within parentheses to point to the attached property within the Binding definition:

<Rectangle x:Name="TheRotatingRectangle" 
            Fill="Orange"
            Width="100"
            Height="30"
            RenderTransformOrigin="0.5,0.5">
    <Rectangle.RenderTransform>
        <RotateTransform 
          Angle="{Binding
                   Path=(SpinningWithAP:AttachedProperties.RotationAngle),
                   Mode=OneWay,
                   ElementName=TheRotatingRectangle}"/>
    </Rectangle.RenderTransform>
</Rectangle>

You can see that the visual element does not have to be a custom control, we can use an attached property on an element built into Silverlight – Rectangle.

Finally, as was previously stated, due to a Silverlight limitation, we have to specify the storyboard's TargetProperty within the C# code. We can do this in the MainPage constructor as shown in the following snippet:

public MainPage()
{
    InitializeComponent();

    Storyboard rotationStoryboard = 
        (Storyboard) this.Resources["RotationStoryboard"];

    Storyboard.SetTargetProperty
    (
    rotationStoryboard, 
    new PropertyPath(AttachedProperties.RotationAngleProperty)
    );
}