Reset objects to pre-animation values in Silverlight
Is there somethin开发者_开发技巧g built in to reset all completed objects' animations to their pre-animation values?
What I'm doing is loading a usercontrol in a canvas, which runs a bunch of animations when added. Later, I remove the usercontrol (Canvas.Children.Clear), but don't delete it. If I add it back into the canvas again, I want it to appear as if it was re-loaded (i.e. the objects all start from their pre-animated values again and start animating), but the control is not reloading, it's just showing again.
Like a rectangle is visibility.collapsed and canvas.left = 10 as it's pre-animation state and visibility.visible and canvas.left = 50 as it's post animation state. When adding it the canvas, either on load the first time or later just by adding it, I want it to go to it's pre-animation state.
Is there any straight-forward way to do this?
These animations will be children of one or more Storyboard
objects. You need to stop these storyboards in the Unloaded
event of the user control.
What most developers appear to be missing is that a Storyboard
without a duration will run indefinitely. This is missed because the results of this indefinite running are often desirable. For example an animation fades out an element, the developer wants it remain faded out. If the timeline to which the animation belongs is stopped the element's opacity property will snap back to the assigned value prior to the animation starting.
In your case you also want the Storyboards of your animations to run indefinitely while the Usercontrol is loaded. However to get the effect you desire you need to stop these storyboards in the unloaded event.
Its easy enough to stop a Storyboard, it has a Stop
method. The trick is to get hold of the storyboard. The easiest way it so give the storyboard a x:Name
. Now you can do something like this:-
public MyUserControl()
{
InitializeComponent();
Unloaded += MyUserControl_Unloaded;
}
void MyUserControl_Unloaded(object sender, RoutedEventArgs e)
{
LoadedStoryboard.Stop();
}
where LoadedStoryboard
is the value of x:Name
for the storyboard that runs the loaded animations.
Within WPF you can 'undo' an animation by applying an animation that sets the target property to null. See the following blog post:
http://joshsmithonwpf.wordpress.com/2008/08/21/removing-the-value-applied-by-an-animation/
This might work in Silverlight as well.
精彩评论