views:

430

answers:

1

I'm running into a really weird issue with Silverlight 3. I've defined an extension method to create a storyboard around a given DoubleAnimation and play that animation. I know the default duration for storyboards in Silverlight is 1s, but I would like to change that for my animations. However, when I set a different duration, instead of altering the animation to move from start to finish in the allotted time, the animation will just play for the duration I specified and then stop. For example, if I'm moving something from 0,0 to 0,10 and set the duration to .3s, the item will only move to 0.3. I can't imagine this is by design. Any ideas what is going on here?

Here's the code I'm using. ConfigureStoryboard is where the storyboard is created around the animation. I've removed some code regarding the easing function to make it more readable.

    public static void BeginAnimation(
        this Transform transform,
        DependencyProperty property,
        DoubleAnimation animation,
        EasingFunction function
    )
    {
        var storyboard = new Storyboard();
        ConfigureStoryboard(animation, storyboard, function);
        Storyboard.SetTarget(storyboard, transform);
        Storyboard.SetTargetProperty(
          storyboard,
          new PropertyPath(property));

        storyboard.Begin();
    }

    private static void ConfigureStoryboard(DoubleAnimation animation, Storyboard storyboard, EasingFunction function)
    {
        DoubleAnimation myAnimation = new DoubleAnimation();
        storyboard.Duration = animation.Duration;
        myAnimation.From = animation.From;
        myAnimation.To = animation.To;

        storyboard.Children.Add(myAnimation);
    }
+1  A: 
    private static void ConfigureStoryboard(DoubleAnimation animation, Storyboard storyboard, EasingFunction function)
    {
        DoubleAnimation myAnimation = new DoubleAnimation();
        myAnimation.Duration = animation.Duration;
        myAnimation.From = animation.From;
        myAnimation.To = animation.To;

        storyboard.Children.Add(myAnimation);
    }

Animation needs to have a duration else it get the default, and since the storyboard will only run for 0.3s, only a third of your animation will run before stopping.

Graeme Bradbury
The animation does have a duration set. If I forget to set that, the storyboard will just run for 1s. With the duration set to <1s, it will only play for that long before halting prior to completion.
oltman
You need to set BOTH the Animation's Duration and the Storyboard's Duration. If you have a storyboard 0.3 seconds long that contains an animation that is 1 second long, only the first 0.3 seconds of the animation will be played.
KeithMahoney
Perfect! Thank you very much!
oltman