views:

188

answers:

1

Hi, I'm trying to do smooth animation in procedural code. For this (in Silverlight at least), it's recommended to use the Storyboard timer rather than a DispatcherTimer. So I use something like this:

    Storyboard _LoopTimer = new Storyboard();
    public void StartAnimation()
    {
        _LoopTimer.Duration = TimeSpan.FromMilliseconds(0);
        _LoopTimer.Completed += new EventHandler(MainLoop);
        _LoopTimer.Begin();
    }
    void MainLoop(object sender, EventArgs e)
    {
        // Do animation stuff here

        // Continue storyboard timer
        _LoopTimer.Begin();
    }

And in Silverlight, this works fine. But in WPF, I only hit MainLoop() once. Setting RepeatBehaviour to Forever doesn't help, either. So what's the right way to do this in WPF with a Storyboard?

Thanks very much.

A: 

I thought that using a Storyboard in Silverlight was just a hack to make up for there being no DispatcherTimer in the early versions.

In any case, you need to restart your Storyboard once it has finished. It will not automatically restart itself.

Mark Heath
Don't know of its historical origins. But it's recommended in several places that Storyboard gives smoother animation than DispatcherTimer. (I was previously using DispatcherTimer myself and it was indeed quite jerky.)As you say, to get the repeating effect you need the _LoopTimer.Begin() in MainLoop(). But this only works in silverlight. In WPF it still doesn't loop. So how do I make it loop in WPF? That's my question.
Adrian
I see, I'm not sure why it doesn't fire again in WPF. possibly to do with there not being any children to the storyboard?
Mark Heath