Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method after a Storyboard finished?

Tags:

c#

wpf

storyboard

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.

like image 434
frank_funk Avatar asked Sep 07 '11 11:09

frank_funk


1 Answers

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.

like image 189
ChrisF Avatar answered Oct 10 '22 09:10

ChrisF