3

我希望有人可以帮助我解决我认为相对简单的问题。

我正在使用 DoubleAnimation 对象在代码中设置淡出动画。它淡出图像,然后在完成后触发 Completed 事件。

我想从事件处理程序中获取应用淡出动画的控件的名称,但我找不到方法。

任何帮助表示赞赏。谢谢。

DispatcherTimer timer = new DispatcherTimer();

public MainWindow()
{
    InitializeComponent();

    image1.Visibility = System.Windows.Visibility.Visible;
    image2.Visibility = System.Windows.Visibility.Collapsed;

    timer.Interval = TimeSpan.FromSeconds(2);
    timer.Tick += new EventHandler(timer_Tick);
    timer.Start();
}

void FadeOut(UIElement element)
{
    DoubleAnimation FadeOut = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(0.5)));
    FadeOut.Completed += new EventHandler(FadeOut_Completed);
    element.BeginAnimation(OpacityProperty, FadeOut);
}

void FadeOut_Completed(object sender, EventArgs e)
{
    // How to find out which control was targeted?
}

void timer_Tick(object sender, EventArgs e)
{
    if (image1.Visibility == System.Windows.Visibility.Visible)
    {
        FadeOut(image1); 
        //image1.Visibility = System.Windows.Visibility.Collapsed;
        //image2.Visibility = System.Windows.Visibility.Visible;
    }
}
4

1 回答 1

5

以下代码为您提供完成动画的目标。将它放在 FadeOut_Completed() 处理程序中:

DependencyObject target = Storyboard.GetTarget(((sender as AnimationClock).Timeline as AnimationTimeline))

但是,这仅在指定动画目标对象时才有效。为此,将以下内容添加到 FadeOut() 方法:

Storyboard.SetTarget(FadeOut, element);
于 2012-04-30T12:39:23.000 回答