尝试使用不同的Completed-actions多次触发WPF StoryBoard

时间:2013-03-26 16:33:42

标签: c# wpf animation storyboard blend

我在代码背后制作故事板并且多次链接到彼此运行它是不成功的。不知何故,似乎故事板保持在上下文中,并且不会重置。

我正在设置几个元素的动画,并且我多次递归运行动画方法,但在Completed事件中使用不同的回调动作。第一个动画运行正常,但其余部分根本没有动画(已完成的事件触发)。

如果我在一个方法中创建一个StoryBoard并运行它,它是否应该在完成之后处理?我正在尝试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();
}

谢谢你的时间!

1 个答案:

答案 0 :(得分: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();