每次按下按钮“WPF动画”时,我如何重播相同的动画
Posted
技术标签:
【中文标题】每次按下按钮“WPF动画”时,我如何重播相同的动画【英文标题】:How can i replay the same animation every time button pressed " WPF Animmation" 【发布时间】:2016-01-16 18:15:24 【问题描述】:如何在每次按下按钮时重播相同的动画第一次按下 btn 动画播放但 nxt 存在运行时错误
private void Button_Click(object sender, RoutedEventArgs e)
int j = 0;
string[] names ="/Assets/1.png", "/Assets/2.png", "/Assets/3.png", "/Assets/4.png", "/Assets/5.png" ;
var storyboard = new Storyboard
;
var animation = new ObjectAnimationUsingKeyFrames();
Storyboard.SetTarget(animation,img);
Storyboard.SetTargetProperty(animation, new PropertyPath("Source"));
storyboard.Children.Add(animation);
for (int i=0 ; i <=4; i++)
// j = j + 1;
var keyframe = new DiscreteObjectKeyFrame
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(300* i)),
Value = String.Format(names[i])
;
animation.KeyFrames.Add(keyframe);
Resources.Add("Storyboard", storyboard);
// Resources.Add("Storyboard", storyboard);
storyboard.Begin();
// storyboard.Completed += new EventHandler(Story_Completed);
// Thread.Sleep(1000);
if (j==4)
storyboard.Pause();
【问题讨论】:
【参考方案1】:由于您没有提供有关异常的详细信息,因此很难判断错误来自何处,但我看到至少有一件事情可能无法正常工作:
Resources.Add("Storyboard", storyboard);
这第一次会起作用,但第二次会抛出异常(因为情节提要已经添加到资源中)。
无论如何,如果您采取一些预防措施,您可以重复使用相同的故事板对象:
首先,让我们将故事板存储在一个属性中,因为它比资源更容易操作:
private Storyboard ButtonStoryboard get; set;
点击按钮时,我们首先检查storyboard是否存在。如果没有,我们创建它:
if (this.ButtonStoryboard == null)
string[] names ="/Assets/1.png", "/Assets/2.png", "/Assets/3.png", "/Assets/4.png", "/Assets/5.png" ;
var storyboard = new Storyboard();
var animation = new ObjectAnimationUsingKeyFrames();
Storyboard.SetTarget(animation,img);
Storyboard.SetTargetProperty(animation, new PropertyPath("Source"));
storyboard.Children.Add(animation);
for (int i=0 ; i <=4; i++)
var keyframe = new DiscreteObjectKeyFrame
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(300* i)),
Value = String.Format(names[i])
;
animation.KeyFrames.Add(keyframe);
this.ButtonStoryboard = storyboard;
现在我们必须启动情节提要,如果它还没有运行(你不能启动它两次)。如果它已经在运行,我们将其停止并倒带(就像录像带一样):
if (this.ButtonStoryboard.GetCurrentState() != ClockState.Stopped)
this.ButtonStoryboard.Stop();
this.ButtonStoryboard.Seek(TimeSpan.Zero);
this.ButtonStoryboard.Begin();
综合起来:
private Storyboard ButtonStoryboard get; set;
private void Button_Click(object sender, RoutedEventArgs e)
if (this.ButtonStoryboard == null)
string[] names ="/Assets/1.png", "/Assets/2.png", "/Assets/3.png", "/Assets/4.png", "/Assets/5.png" ;
var storyboard = new Storyboard();
var animation = new ObjectAnimationUsingKeyFrames();
Storyboard.SetTarget(animation,img);
Storyboard.SetTargetProperty(animation, new PropertyPath("Source"));
storyboard.Children.Add(animation);
for (int i=0 ; i <=4; i++)
var keyframe = new DiscreteObjectKeyFrame
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(300* i)),
Value = String.Format(names[i])
;
animation.KeyFrames.Add(keyframe);
this.ButtonStoryboard = storyboard;
if (this.ButtonStoryboard.GetCurrentState() != ClockState.Stopped)
this.ButtonStoryboard.Stop();
this.ButtonStoryboard.Seek(TimeSpan.Zero);
this.ButtonStoryboard.Begin();
【讨论】:
以上是关于每次按下按钮“WPF动画”时,我如何重播相同的动画的主要内容,如果未能解决你的问题,请参考以下文章