How to animate at periodic intervals?

2019-07-23 19:53发布

I have a storyboard which animates a minute hand to slide 6 degrees. Now i want the minute hand to slide at every 59th second forever. Are there any properties of storyboard or any other way that i can do it?

My storyboard

<Storyboard
                                    x:Name="myStoryboard2">
                                    <DoubleAnimation
                                        x:Name="minuteAnimation"
                                        Storyboard.TargetName="minHandTransform"
                                        Storyboard.TargetProperty="Angle"
                                        Duration="0:0:1"
                                        From="{Binding Time, Converter={StaticResource minuteHandTransform}}"
                                        To="{Binding Time, Converter={StaticResource minuteHandTransform}}"
                                        RepeatBehavior="1x">
                                        <DoubleAnimation.EasingFunction>
                                            <SineEase
                                                EasingMode="EaseOut" />
                                        </DoubleAnimation.EasingFunction>
                                    </DoubleAnimation>
                                </Storyboard>

2条回答
Luminary・发光体
2楼-- · 2019-07-23 20:46

It doesn't sound like something you would want to rely on an animation to manage. Simply manage starting the animation from code behind every minute and you are done. It would be a lot easier to do it that way than using cryptic converters to control the From/To values. A Timeline such as a DoubleAnimation has a BeginTime property, but I have seen and verified reports of long animation durations (like 1 minute or above) hitting bugs in WinRT.

EDIT* (code samples)

Two simple ways I commonly use to trigger events at an interval are to use a DispatcherTimer with callback events or an async loop.

1.  DispatcherTimer

var timer = new DispatcherTimer { Interval = TimeSpane.FromSeconds(1) };
timer.Tick += (s, e) => { /* do your stuff */ };
timer.Start();

2.  async loop

RunMyLoop();

private async void RunMyLoop()
{
    while (true)
    {
        /* do your stuff */
        await Task.Delay(1000);
    }
}
查看更多
贪生不怕死
3楼-- · 2019-07-23 20:50

Try the following:

<Storyboard
                                x:Name="myStoryboard2">
                                <DoubleAnimation
                                    x:Name="minuteAnimation"
                                    Storyboard.TargetName="minHandTransform"
                                    Storyboard.TargetProperty="Angle"
                                    Duration="0:0:59"
                                    From="{Binding Time, Converter={StaticResource minuteHandTransform}}"
                                    To="{Binding Time, Converter={StaticResource minuteHandTransform}}"
                                    RepeatBehavior="Forever">
                                    <DoubleAnimation.EasingFunction>
                                        <SineEase
                                            EasingMode="EaseOut" />
                                    </DoubleAnimation.EasingFunction>
                                </DoubleAnimation>
                            </Storyboard>
查看更多
登录 后发表回答