在完成的事件Windows 8应用程序中从故事板获取UIElement

时间:2013-08-23 21:31:00

标签: c# animation windows-8 microsoft-metro winrt-xaml

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

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事件中完成了哪个Image。

1 个答案:

答案 0 :(得分:7)

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

示例:

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

 ....

private void StoryboardCompleted(Storyboard storyboard, Image image)
{

}