每次按下按钮时如何重放相同的动画" WPF Animmation"

时间:2015-10-18 10:20:47

标签: c# xaml animation windows-phone

我每次按下按钮时如何重播相同的动画第一次按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 个答案:

答案 0 :(得分:0)

由于您没有提供有关异常的详细信息,因此很难说出错误的来源,但我发现至少有一件事情无法发挥作用:

Resources.Add("Storyboard", storyboard);

这将是第一次工作但第二次会抛出异常(因为故事板已经添加到资源中)。

在任何情况下,如果采取一些预防措施,您可以重复使用相同的故事板对象:

首先,让我们将故事板存储在一个属性中,因为它比资源更容易操作:

private Storyboard ButtonStoryboard { get; set; }

单击按钮时,我们首先检查故事板是否存在。如果没有,我们创建它:

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