【问题标题】:Get target control from DoubleAnimation Completed event in WPF?从 WPF 中的 DoubleAnimation Completed 事件获取目标控件?
【发布时间】:2012-05-09 21:50:40
【问题描述】:

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

我正在使用 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;
    }
}

【问题讨论】:

  • 发件人?这个动画的目标没有明确设置,所以如果它有效,它肯定是发件人
  • 如何明确设置目标? PS。更新了代码以包含计时器滴答事件。
  • 我有点错,但找到了解决方案。看我的回答

标签: wpf animation


【解决方案1】:

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

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

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

Storyboard.SetTarget(FadeOut, element);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多