如何在Windows Core IoT应用程序中显示带有当前时间的时钟? [英] How to display a clock with the current time in a Windows Core IoT app?
问题描述
我正在尝试创建一个在Raspberry Pi 2上无头运行的Windows 10 IoT应用程序. 一切都已正确设置,并且我可以使用Raspberry Pi作为调试的远程计算机从Visual Studio调试我的计算机.
I am trying to create an Windows 10 IoT app running headless on a Raspberry Pi 2. Everything is set up correctly and I am able to debug my from Visual Studio using the Raspberry Pi as the remote machine for debugging.
现在,我想在应用页面上添加时钟,但是我不知道如何保持显示的时间更新. 在普通的旧C#中,我将使用BackgroudWorker或类似的工具来使显示的时间保持最新,但是UWP中没有该类型.
Now I want to add a clock on the app page but I can't figure out how to keep the displayed time updated. In plain old C# I would use a BackgroudWorker or something similar to keep the displayed time current, but that type is not available in UWP.
我一直在研究创建并注册一个MSDN上的后台任务" ,但这似乎很复杂,只是为了使显示时间保持最新.
I have been looking into "Create and register a background task" on MSDN but that seems to be way to complex, just to be able to keep the display time up to date.
所以我的问题是:我怎样才能以最简单的可行方式创建一个时钟显示,该时钟显示在每次滴答滴答时都会更新?
So my question is: How can I - in the simplest posible way - create a clock display that is updated every time the time ticks?
推荐答案
如果应用无头运行,则必须将数据设置到显示设备(LCD,LED等). 对于带头的应用程序,您将使用XAML页面显示时钟. 您可以使用计时器在每次发生时间跨度时得到通知.
If your app is running headless, you will have to set data to a display device (LCD, LEDs, etc.). With a headed app, you will use a XAML page to display the clock. You can use a timer to get notified every time span occurs.
计时器声明
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参考文档
ThreadPoolTimer reference documentation
请记住,Raspberry Pi没有电池可以节省当前时间.您的开发板将必须通过Internet进行同步以更新其日期/时间.
Keep in mind that a Raspberry Pi does not have a battery to save the current time. Your board will have to sync through the Internet to update its date/time.
这篇关于如何在Windows Core IoT应用程序中显示带有当前时间的时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!