0

I'm unsuccessful in making a storyboard in code behind and running it multiple times chained to each other. Somehow, it seems the storyboard keeps in context, and will not reset.

I'm animating several elements, and X number of times I'm recursively running the animation-method, but with different call-back actions in the Completed event. First animation runs fine, but the rest it doesn't animate at all (the completed-event fires).

If I create a StoryBoard in a method and run it, should it not be disposed after it is completed? I'm trying to do storyboard.Remove().

private void SlideLeft(int numberOfStepsToSlide)
{
    if (numberOfStepsToSlide < 1) return;
    Slide(() => SlideLeft(numberOfStepsToSlide - 1));
}

protected void Slide(Action callBackAfterAnimation = null)
{
    var sb = new Storyboard();
    sb.FillBehavior = FillBehavior.Stop; //i thought maybe this would fix it, but no

    //..
    //.. a number of double animations created and added to storyboard
    //..

    sb.Completed += (sender, e) =>
                        {
                            sb.Stop();
                            sb.Remove();

                            //..
                            //..sending message to ViewModel and manipulating values
                            //..

                            if (callBackAfterAnimation != null)
                                callBackAfterAnimation();
                        };
    sb.Begin();
}

Thanks for your time!

4

1 回答 1

0

对不起,完全忘记了这个问题!

你不想打电话Remove- 这基本上会杀死动画,通过杀死所有为运行它而创建的动画时钟......试试这样的东西(快速和肮脏的例子):

var win = new Window();
win.Width = 50;
win.Height = 50;

int runCount = 3;
int halfSteps = runCount * 2;
double toWidth = 500.0;

var sb = new Storyboard();  
var biggerator = new DoubleAnimation(toWidth, new Duration(TimeSpan.FromSeconds(2)));
sb.Children.Add(biggerator);
Storyboard.SetTarget(biggerator, win);
Storyboard.SetTargetProperty(biggerator, new PropertyPath("Width"));

sb.Completed += (o,e) => 
{ 
    sb.Stop();          
    halfSteps--;
    if(halfSteps <= 0)
    {
        win.Height = 150;
    }
    else
    {
        biggerator.To = biggerator.To == 0 ? toWidth : 0;
        sb.Begin();
    }
};

sb.Begin();     
win.Show();
于 2013-04-03T20:44:24.780 回答