1

I'm creating a fadeIn/fadeOut animation from code behind. I was trying to stop animation at a particular blink time and it is working fine. But I want to make sure that when my animation stops it should stop in fadeOut state. Below is my code:

public void AddAnimation(long blinkDuration = 0)
        {
            var fadeIn = new DoubleAnimation(0.3, 1, TimeSpan.FromSeconds(1), FillBehavior.HoldEnd)
            {
                BeginTime = TimeSpan.FromSeconds(0)
            };

            var fadeOut = new DoubleAnimation(1.0, 0.3, TimeSpan.FromSeconds(1), FillBehavior.HoldEnd)
            {
                BeginTime = TimeSpan.FromSeconds(0.5)
            };

            storyboard = new Storyboard();
            Storyboard.SetTarget(fadeIn, this);
            Storyboard.SetTarget(fadeOut, this);
            Storyboard.SetTargetProperty(fadeIn, new PropertyPath("(Opacity)"));
            Storyboard.SetTargetProperty(fadeOut, new PropertyPath("(Opacity)"));
            storyboard.RepeatBehavior = blinkDuration == 0
                                            ? RepeatBehavior.Forever
                                            : new RepeatBehavior(new TimeSpan(0, 0, Convert.ToInt32(blinkDuration)));
            storyboard.Children.Add(fadeIn);
            storyboard.Children.Add(fadeOut);
            storyboard.Begin();
        }

My question is how I will make my icon state fadeOut on Storyboard Stop after blink interval.

2 Answers 2

2

I have fixed it. I changed my Storyboard.Begin call and registered storyboard.Completed event, on which I'm calling storyboard.Stop().

Below is my changed code:

storyboard.Completed += StoryboardCompleted;
storyboard.Begin(this, true);

void StoryboardCompleted(object sender, EventArgs e)
{
   storyboard.Stop(this);
}
0

You can use a FillBehaviour property value of HoldEnd which will keep the last value (or the To value) that you set in your Storyboard. This way, you can ensure what the end value will be. The only problem that you may have using this property is that you may need to run your animation several times.

To do that, you can attach a handler to the Completed Event and re-run the Storyboard after it finishes if a certain condition is met (number of times run, or seconds passed, or data has arrived, etc.):

private void OnStoryboardCompleted(object sender, EventArgs e)
{
    if (someConditionIsTrue) storyboard.Start();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.