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

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

现在我想在应用页面上添加一个时钟,但我无法弄清楚如何更新显示的时间。 在普通的旧C#中,我会使用BackgroudWorker或类似的东西来保持显示的时间是最新的,但是UWP中没有这种类型。

我一直在MSDN上查看“创建并注册后台任务” ,但这似乎是复杂的,只是为了能够使显示时间保持最新。

所以我的问题是:我怎样才能 – 以最简单的方式 – 创建一个时钟显示,每次时间点都会更新?

如果您的应用程序无头运行,则必须将数据设置为显示设备(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同步才能更新其日期/时间。