我希望有人可以帮助我解决我认为相对直接的问题。
我正在使用DoubleAnimation对象在代码中设置淡出动画。它淡出图像,然后在完成时触发完成事件。
我想从事件处理程序中获取应用了淡出动画的控件的名称,但我找不到方法。
任何帮助表示赞赏。感谢。
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;
}
}
答案 0 :(得分:5)
以下代码为您提供已完成动画的目标。将它放在FadeOut_Completed()处理程序中:
DependencyObject target = Storyboard.GetTarget(((sender as AnimationClock).Timeline as AnimationTimeline))
但是,这仅在指定动画目标对象时才有效。为此,将以下内容添加到FadeOut()方法:
Storyboard.SetTarget(FadeOut, element);