3

我有一个我正在使用情节提要制作动画的图像。我希望能够在情节提要动画完成后更改图像的来源。但是,我不知道如何在 Completed 事件处理程序中访问 Image。如何通过 Storyboard 对象访问图像。

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?
}

我用多个图像多次制作这个动画,所以我不能简单地制作一个成员变量。我需要知道在 Completed 事件中完成了哪个图像。

4

1 回答 1

9

您可以对事件使用 Lambda 表达式,以便可以将图像作为参数传递

例子:

sb.Completed += (s,e) => StoryboardCompleted(sb, i);

 ....

private void StoryboardCompleted(Storyboard storyboard, Image image)
{

}
于 2013-08-23T22:03:39.763 回答