I want my app to animate textblock one by one. When one animation is finished the next should start. When this line if (hasEnded == true | x==0) is commented it works (but its whole text not single line), if not application freezes. This is my code.
private void Animatebtn_Click(object sender, RoutedEventArgs e)
{
if (saccades != null)
{
LinearGradientBrush colorBrush = LoadBrush();
for (int x = 0; x < textArray.Length;)
{
if (hasEnded == true | x==0)
{
var temp = (TextBlock)this.FindName("tb" + x);
mytb = temp;
mytb.Foreground = colorBrush;
sb = Animate(sbduration, aduration, abegintime);
sb.Completed += new EventHandler(Story_Completed);
sb.Begin(this); // starts the animation
hasEnded = false;
x++;
}
}
}
private void Story_Completed(object sender, EventArgs e)
{
hasEnded = true;
}
And this is my animation:
public Storyboard Animate(double sBoardduration, double animationDuration, double animationBeginTime)
{
Duration storybDuration = TimeSpan.FromSeconds(sBoardduration);
TimeSpan animBegintime = TimeSpan.FromSeconds(animationBeginTime);
Duration animDuration = TimeSpan.FromSeconds(animationDuration);
Storyboard sb = new Storyboard() { Duration = storybDuration};
DoubleAnimation DA = new DoubleAnimation()
{ By = 1, Duration = animDuration, BeginTime = animBegintime };
DoubleAnimation DA2 = new DoubleAnimation()
{ By = 1, Duration = animDuration, BeginTime = animBegintime };
sb.Children.Add(DA); sb.Children.Add(DA2);
Storyboard.SetTargetName(DA, "redStop");
Storyboard.SetTargetName(DA2, "BlackStop");
Storyboard.SetTargetProperty(DA, new PropertyPath("Offset"));
Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset"));
return sb;
}