故事板完成后如何调用方法? [英] How to call a method after a Storyboard finished?

查看:30
本文介绍了故事板完成后如何调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下代码:

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 与对方法的调用结合起来?

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?

非常感谢.

推荐答案

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

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

这将在 Storyboard 完成时执行.

This will get executed when the Storyboard completes.

这篇关于故事板完成后如何调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆