如何在故事板完成后调用一个方法

本文关键字:方法 一个 调用 故事 | 更新日期: 2023-09-27 18:07:56

我写了下面的代码:

public void name(object sender, RoutedEventArgs e)
    {
        DoubleAnimation myDoubleAnimation = new DoubleAnimation();
        myDoubleAnimation.From = 1.0;
        myDoubleAnimation.To = 0.0;
        myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.2));
        sb1 = new Storyboard();
        sb1.Children.Add(myDoubleAnimation);
        Storyboard.SetTargetName(myDoubleAnimation, one.Name);
        Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Grid.OpacityProperty));
        sb1.Begin(this);
        if (one.Opacity == 0)
        {
            Container_one.Children.Remove(one);
        }     
    }

,但它不能正常工作。动画工作正常,但删除是错误的。我如何将Storyboard-End与方法的调用结合起来?

如何在故事板完成后调用一个方法

由于Storyboard的执行是异步的,您需要添加一个"Storyboard Completed"事件处理程序:

story.Completed += new EventHandler(Story_Completed);

然后把你的Remove代码放在里面:

private void Story_Completed(object sender, EventArgs e)
{
    if (one.Opacity == 0)
    {
        Container_one.Children.Remove(one);
    }
}

将在Storyboard完成时执行