首页 文章

我该如何暂停故事板?

提问于
浏览
2

这似乎应该是一个明智的选择,但我无法暂停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);

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

如何让故事板暂停?

1 回答

  • 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();
    

相关问题