Search code examples
c#xamlanimationwindows-phone

How can i replay the same animation every time button pressed " WPF Animmation"


How can i replay the same animation every time button pressed 1st time i press btn animation play but nxt there is runtime error

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

        }

        }

Solution

  • It's hard to tell where the error comes from since you don't give details about the exception, but I see at least one thing that cannot possibly work:

    Resources.Add("Storyboard", storyboard);
    

    This will work the first time but will throw an exception the second time (since the storyboard has already been added to resources).

    In any case, you can re-use the same storyboard object if you take some precautions:

    First, let's store the storyboard in a property, because it's easier to manipulate than resources:

    private Storyboard ButtonStoryboard { get; set; }
    

    When clicking on the button, we first check whether the storyboard exists. If not, we create it:

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

    Now we must start the storyboard only if isn't already running (you can't start it twice). If it's already running, we stop it and rewind it (just like a videotape):

    if (this.ButtonStoryboard.GetCurrentState() != ClockState.Stopped)
    {
        this.ButtonStoryboard.Stop();
        this.ButtonStoryboard.Seek(TimeSpan.Zero);
    }
    
    this.ButtonStoryboard.Begin();
    

    Putting all together:

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