c# - 如何定期制作动画?

标签 c# xaml windows-8 windows-runtime winrt-xaml

我有一个 Storyboard,可以使分针滑动 6 度。现在我希望分针永远每 59 秒滑动一次。 Storyboard有什么属性或我可以做的任何其他方式吗?

我的 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>

最佳答案

这听起来不像是您想要依靠动画来管理的东西。只需管理每分钟从代码后面启动动画即可完成。这样做比使用神秘的转换器来控制 From/To 值要容易得多。 DoubleAnimation 等时间轴具有 BeginTime 属性,但我已经看到并验证了长动画持续时间(例如 1 分钟或以上)在 WinRT 中遇到错误的报告。

编辑*(代码示例)

我通常用来按时间间隔触发事件的两种简单方法是使用带有回调事件或异步循环的 DispatcherTimer。

1.调度定时器

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

2.异步循环

RunMyLoop();

private async void RunMyLoop()
{
    while (true)
    {
        /* do your stuff */
        await Task.Delay(1000);
    }
}

关于c# - 如何定期制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14273506/

相关文章:

c# - Microsoft JScript 运行时错误 : Unable to get value of the property 'style' : object is null or undefined

C# 从带引号的字符串中删除分隔符

c# - PDF 下载失败显示消息 "Couldn' t be downloaded“仅在 IE11 中

wpf - 将文本框放置在 Canvas 上

javascript - WinJS 在导航时保留元素 ID

c# - 如何在 Windows 8 Metro 应用程序中打开托管的 PDF?

xaml - 如何在 Windows 应用商店应用程序 (Windows 8.1) 中设置 GridViewItem/GridViewItemPresenter 的样式?

c# - 如何以秒为单位创建时间戳

wpf - 如何在XAML中制作简单的超链接?

c# - 使用带有 HierarchicalDatatemplate 的附加行为