I want to replace WPF Storyboard with some custom WHILE loop to check if it is possible. The code I have is the following one.
public void RestartAnimation(int seconds)
{
da.From = this.ActualWidth;
da.To = -ContentActualWidth;
da.RepeatBehavior = RepeatBehavior.Forever;
da.Duration = new Duration(TimeSpan.FromSeconds(seconds));
Timeline.SetDesiredFrameRate(da, 15);
if (sb.Children.Count == 0)
{
sb.Children.Add(da);
Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Left)"));
Storyboard.SetTarget(da, cp);
sb.Begin();
}
}
I guess it should be some Timer & WHILE inside of it…
Any clue how it could be done?
Thanks!!
I would say, don’t use a while or any other loop, since then you will freeze your program while the Loop is running.
The simplest way (not necessarily the best) would be to set up a timer which executes once or so per frame so 30-120 times a sec and incrementally do your animation detecting how much real time has actually passed in between the timer calls.
So if you need to move an object with the speed of 10 units per second, and your timer executes on some fast interval, I would find the time when the last timer call was made, the present time, find the dT(Time Change), and do dT*10, move the object that number of units.
As the timer iterates, you should see the object move with out freezing your software.