mgagne_98

Hi all,

I¡¯ve now figured out how to use the StoryBoard and the property RepeatBehavior. It works great but I have a small problem that I am not able to solve. I am working on an Infinite Progress Bar animation and when I reach the end of my animation; the animation is repeated (which is OK). However, there is a gap in the animation (because of color fade in, fade out) and I would like the animation to start to repeat X seconds before the end of the previous one. Is it possible

Thanks!

Martin



Re: Windows Presentation Foundation (WPF) RepeatBehavior

mgagne_98

Nobody has an idea to solve my probblem




Re: Windows Presentation Foundation (WPF) RepeatBehavior

Yi-Lun Luo - MSFT

Hello, I think you¡¯ll have to do without RepeatBehavior. You can create more than one Storyboards. Try this:

First begin your animation in code instead of XAML:

private void Window_Loaded(object sender, RoutedEventArgs e)

{

Storyboard sb = (Storyboard)this.Resources["sb"];

sb.CurrentTimeInvalidated += new EventHandler(Storyboard_CurrentTimeInvalidated);

sb.Name = "sb" + currentName.ToString();

currentName++;

continued.Add(sb.Name, false);

sb.Begin(this);

}

Here I used a Dictionary named continued and an integer named currentName. They are:

//This field indicates whether a Storyboard has been continued or not. the string is the Storyboard's name.

private Dictionary<string, bool> continued = new Dictionary<string,bool>();

//We need to give each Storyboard a name. Use this field to assign a name.

private int currentName;

Now we can handle the Storyboard¡¯s CurrentTimeInvalidated event:

private void Storyboard_CurrentTimeInvalidated(object sender, EventArgs e)

{

Clock clock = (Clock)sender;

Storyboard sb = (Storyboard)clock.Timeline;

//This Storyboard hasn't been contined. Let's check if we need to continue it during this tick.

if (!continued[sb.Name])

{

TimeSpan time = clock.CurrentTime;

//Ticks aren't very precisely. We can't ask the Storyboard to do something on exactly the third second. We'll have to use a >= instead of a ==. That's why we need to check if this Storyboard has been continued.

if (time != null && time >= TimeSpan.FromSeconds(3))

{

//Get a copy of this Storyboard and do the same.

Storyboard sb2 = sb.Clone();

sb2.CurrentTimeInvalidated += new EventHandler(Storyboard_CurrentTimeInvalidated);

sb2.Name = "sb" + currentName.ToString();

currentName++;

continued.Add(sb2.Name, false);

sb2.Begin(this);

//Now that we've begun a new Storyboard, sb2, we need to mark sb as continued to prevent our handler from begining another new Storyboard after another tick.

continued[sb.Name] = true;

}

}

}