I wrote the following code. It tries to create a storyboard that does the following:
- fade in for 500ms
- pause for 1000ms
- fade out for 500ms
But at run time get an System.InvalidOperationException followed by the following output:
Additional information: Multiple animations in
the same containing Storyboard cannot target the
same property on a single element.
This seems to suggest its trying to do all the animations at once rather than sequentially.
private Storyboard createStoryboard()
{
Storyboard board = new Storyboard();
addFadeToStoryboard(board, 0, 1, 500);
addFadeToStoryboard(board, 1, 1, 1000);
DoubleAnimation fadeOut = addFadeToStoryboard(board, 1, 0, 500);
fadeOut.Completed += new EventHandler(onFadeCompleted);
Storyboard.SetTarget(board, this);
return board;
}
private DoubleAnimation addFadeToStoryboard(Storyboard board,
double fadeFrom, double fadeTo, double milliseconds)
{
DoubleAnimation fade = new DoubleAnimation()
{
Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds)),
From = fadeFrom,
To = fadeTo,
RepeatBehavior = new RepeatBehavior(1)
};
Storyboard.SetTargetProperty(fade,
new PropertyPath(UIElement.OpacityProperty));
board.Children.Add(fade);
return fade;
}
How can I make it sequential? Am I misinterpreting something fundamental about storyboards?
Thanks
I’ve solved this by creating my own sequencer class. It has a dependency on a LinkedList class, so you’ll have to swap that out with the standard one if you want to use this code: