I have an Image that i'm animating with a Storyboard. I want to be able to change the source of the image after the storyboard animation has completed. However, I don't know how to access the Image in the Completed event handler. How can I gain access to the Image through a Storyboard object.
private void AnimateImage() {
Image i = new Image();
//Set name and source of image here
i.RenderTransform = new CompositeTransform();
Duration d = new Duration(TimeSpan.FromSeconds(2));
DoubleAnimation anim1 = new DoubleAnimation();
DoubleAnimation anim2 = new DoubleAnimation();
anim1.Duration = d;
anim2.Duration = d;
Storyboard sb = new Storyboard();
sb.Duration = d;
sb.Children.Add(anim1);
sb.Children.Add(anim2);
Storyboard.SetTarget(anim1, i);
Storyboard.SetTarget(anim2, i);
Storyboard.SetTargetProperty(anim1, "(UIElement.RenderTransform).(CompositeTransform.TranslateX)");
Storyboard.SetTargetProperty(anim2, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)");
//Set anim1/anim2 To/From properties
sb.Completed += StoryboardCompleted;
sb.Begin();
}
private void StoryboardCompleted(object sender, object o) {
Storyboard sb = (Storyboard) sender;
//How can I gain access to the Image?
}
I'm doing this animation many times with multiple images so I cant simply make a member variable. I need to know which Image is done in the Completed event.
You could use a Lambda expression for the event so you can pass in the Image as an argument
Example:
sb.Completed += (s,e) => StoryboardCompleted(sb, i);
....
private void StoryboardCompleted(Storyboard storyboard, Image image)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With