(X) Hide this Sign up for the upcoming webinar: Switching on the Cloud for Silverlight by Gill Cleeren. March 23rd, 11 am PST (check your local time)
Full info | Sign up | More webinars
Skip Navigation LinksHome / Articles / View Article

BeginAnimation() method for Silverlight

+ Add to SilverlightShow Favorites
1 comments   /   aggregated from Nick on Silverlight and WPF on May 01, 2008  /  original article
(0 votes)
Categories: Tutorials

Yeah, it's been awhile since I've blogged anything, so I thought a nice way to get back in the game was to post a BeginAnimation method.  Sometimes you just want to start a simple animation in code, and the Silverlight way of doing it through storyboards just seems like more code that you feel like typing, which is where the WPF-inspired BeginAnimation comes in.  Turns out you can do a pretty good BeginAnimation API yourself, using C# extension methods -- just drop the following code into your project:

    public static class Helper
        public static void BeginAnimation(this FrameworkElement e, string prop, Timeline t)
            var sb = new Storyboard();
            Storyboard.SetTarget(sb, e);
            Storyboard.SetTargetProperty(sb, prop);

Which can be used:
                var tb = (TextBox)sender;
                var an = new DoubleAnimation();
                an.From = 100;
                an.To = 200;
                tb.BeginAnimation("Height", an);
(Okay, it doesn't handle all the corner cases quite like WPF, in particular calling BeginAnimation more than once on the timeline doesn't work, and the string parameter will need to change to DependencyProperty in SL beta 2 once we fix the Silverlight Storyboard.TargetProperty signature to match WPF, but the extension method does handle the most common case...)



Comments RSS RSS
  • RE: BeginAnimation() method for Silverlight  

    posted by Asteef on Oct 02, 2010 20:12


    if I write  this Code after the Example

    an.From = 200;
    an.To = 300;
    tb.BeginAnimation("Height", an);

    The Storyboard will not go to a new position ,because it save the first value of its tow parameters

    Can you solve this problem?


Add Comment


Please add 5 and 3 and type the answer here: