如何在 Windows Core IoT 应用程序中显示带有当前时间的时钟?

How to display a clock with the current time in a Windows Core IoT app?

我正在尝试在 Raspberry Pi 2 上创建一个 Windows 10 物联网应用 运行 无头。 一切都设置正确,我可以使用 Raspberry Pi 作为调试的远程机器从 Visual Studio 调试我的。

现在我想在应用程序页面上添加一个时钟,但我不知道如何使显示的时间保持更新。 在普通的旧 C# 中,我会使用 BackgroudWorker 或类似的东西来保持显示的时间最新,但这种类型在 UWP 中不可用。

我一直在 MSDN 上研究 "Create and register a background task",但这似乎很复杂,只是为了能够使显示时间保持最新。

所以我的问题是:如何以最简单的方式创建一个时钟显示,每次时间滴答时都会更新?

如果您的应用 运行 无外设,则必须将数据设置到显示设备(LCD、LED 等)。 对于带标题的应用程序,您将使用 XAML 页面来显示时钟。 您可以使用计时器在每次发生时间跨度时收到通知。

定时器声明

ThreadPoolTimer _clockTimer = null;

定时器初始化

_clockTimer = ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick, TimeSpan.FromMilliseconds(1000));

计时器滴答事件

private void _clockTimer_Tick(ThreadPoolTimer timer)
{
    //Update your display. Use a dispatcher if needed
}

ThreadPoolTimer 参考文档

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.system.threading.threadpooltimer.aspx

请记住,Raspberry Pi 没有电池来保存当前时间。您的看板必须通过 Internet 同步才能更新其 date/time。