2011-03-15 88 views
2

这看起来应该是一件容易的事,但我无法让WPF故事板暂停。我打电话暂停,没有任何反应 - 它保持正确的动画。如何暂停故事板?

这里有一个摄制情况:该动画的宽度按钮。如果您单击该按钮,则会在故事板上调用暂停。我希望,只要我点击按钮,其宽度应该停止改变;相反,它的宽度保持正确的动画,就像我从来没有叫过暂停。

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上)。

如何获得storyboad暂停?

回答

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(); 
+0

呵呵。 MSDN文档坚持认为你必须设置一个名称范围,但是你的版本更简单,并且(b)可以工作。我要买它。 – 2011-03-15 00:52:58

+0

我不得不说我有点困惑。我已经5年了编程WPF,这是我第一次见过名称范围&RegisterName ...的用法: -/ – 2011-03-15 00:55:29

+0

看起来像代码更改的神奇组合是(a)使用SetTarget而不是SetTargetName,并且(b)使用无参数'Begin'和'Pause'而不是传递'this'。 NameScope是无害的,但在使用SetTarget时没有必要。 – 2011-03-15 00:57:58