How to call a method after a Storyboard finished?

狂风中的少年 提交于 2020-01-03 09:07:16

问题


i write the following Code:

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);
        }     
    }

but it doesn't wwork correct. The Animation works fine, but the Remove is wrong. How can i combine a Storyboard-End with the call to a methode?

Thnaks a lot.


回答1:


As the execution of the Storyboard is asynchronous you need to add a "Storyboard Completed" event handler:

story.Completed += new EventHandler(Story_Completed);

then put your Remove code in that:

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

This will get executed when the Storyboard completes.



来源:https://stackoverflow.com/questions/7333406/how-to-call-a-method-after-a-storyboard-finished

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!