Jaa


BeginAnimation for Silverlight 2

Here's a version of the BeginAnimation extension method, updated for Silverlight 2:

    static class SilverlightHelpers
    {
        public static void BeginAnimation(this FrameworkElement obj, DependencyProperty property, DoubleAnimation animation)
        {
            var storyboard = new Storyboard();
            storyboard.Children.Add(animation);
            Storyboard.SetTarget(storyboard, obj);
            Storyboard.SetTargetProperty(storyboard, new PropertyPath(property));
            storyboard.Begin();
        }
    }

Cut-and-paste that baby into your project, and through the magic of C# extension methods, you now have a WPF-style BeginAnimation method on FrameworkElement:

            var a = new DoubleAnimation();
            a.To = 200;
            button.BeginAnimation(Canvas.LeftProperty, a);

Like the earlier version, it's not completely the same as WPF in that calling BeginAnimation more than once on the timeline doesn't work, but it's still a handy shortcut for programmatic animations.

Comments