如何暂停故事板?

时间:2011-03-15 00:18:28

标签: wpf animation storyboard

这似乎应该是一个明智的选择,但我无法暂停WPF故事板。我称之为Pause并没有任何反应 - 它保持正确的动画效果。

这是一个repro案例:一个动画宽度的按钮。如果单击该按钮,则会在故事板上调用Pause。我希望,一旦我点击按钮,它的宽度应该停止变化;相反,它的宽度在动画上保持正确,好像我从未调用过Pause。

NameScope.SetNameScope(this, new NameScope());
var storyboard = new Storyboard();

var button = new Button { Content = "Pause", Name = "pause" };
this.Content = button;
RegisterName(button.Name, button);
var animation = new DoubleAnimation(0, 200, TimeSpan.FromSeconds(5));
Storyboard.SetTargetName(animation, button.Name);
Storyboard.SetTargetProperty(animation,
    new PropertyPath(FrameworkElement.WidthProperty));
storyboard.Children.Add(animation);

button.Click += (sender, e) => { storyboard.Pause(this); };
storyboard.Begin(this);

根据我对文档的理解,我应该使用与Pause(FrameworkElement)相同的参数调用Begin重载,因此上面是Pause(this)。但我也试过storyboard.Pause(),行为没有变化。我也试过storyboard.Pause(button)只是为了它,再次没有效果。我本来试过storyboard.Pause(storyboard)storyboard.Pause(animation)只是为了耗尽可能性,但是没有人编译 - 它需要一个FrameworkElement(或FrameworkContentElement)。

如何让故事情节暂停?

1 个答案:

答案 0 :(得分:4)

我不知道你为什么要使用那个Weired SetNameScope等。清除你的代码我可以让它工作:

        //NameScope.SetNameScope(this, new NameScope());
        var storyboard = new Storyboard();

        var button = new Button { Content = "Pause", Name = "pause" };
        this.Content = button;
        //RegisterName(button.Name, button);
        var animation = new DoubleAnimation(0, 200, TimeSpan.FromSeconds(5));
        Storyboard.SetTarget(animation, button);
        Storyboard.SetTargetProperty(animation,
            new PropertyPath(FrameworkElement.WidthProperty));
        storyboard.Children.Add(animation);

        button.Click += (sender, e) => { storyboard.Pause(); };
        storyboard.Begin();