views:

114

answers:

1

Hi,

How would I reverse a double animation in Silverlight on an event? For example, lets say I have an ellipse as a path and I am moving a shape along this path in an infinite loop. If I press a button, I want to reverse the direction of the spin (clockwise <-> counterclockwise).

To be more specific, I am using the PathListBox object that is new in SL4. It has a start property from 0 to 100% (double 0 - 1) that will place the first item at the specified location along the path. By animating this property from 0 to 1 and 1 to 0, I can make an object spin clockwise and counter clockwise.

Lets say I have a button and on the button press, I want to reverse the direction. How do I go about doing it? I've tried pausing the animation and swapping the To and From values of the animation and resuming it, but this causes the animation to jump. So if its at "3 o'clock" on the ellipse, it jumps to "9 o'clock" and goes backwards from there. I tried doing something like setting the storyboard's CurrentTime property to "Duration - CurrentTime", but that property doesn't seem to be settable.

So, in conclusion, any ideas about how to reverse an animation on demand?

PS: I know there is a PathListBoxUtils that provides scroll behaviors for PathListBox, but that doesn't quite implement what I want. Or to put it another way, I am looking to modify the PathListBoxUtils to have an infinitely rotating, reversible carousel rather than scrolling one item at a time.

+1  A: 

Ok, the way I got it working was by pausing the animation, swapping the To and From values, and using the Seek method to move the animation forward by a set amount. The amount I needed it to move forward is "Duration.TimeSpan() - GetCurrentTime()", but accessing Duration seems to throw an exception (Operation not valid for the current state of the object).

For now, I just hard coded the Duration value for a test and seems to be working fine. I guess for a real implementation the Duration can be bound to some value and the calculation can use that value too.

Chaitanya
I did more or less the same in a similar situation. My problem with getting the `StoryBoard.Duration` was that the value was `Automatic` and I had to navigate to the actual animation classes in the storyboard and get their durations.
Martin Liversage