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

标签 c# win-universal-app raspberry-pi2 iot windows-10-universal

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

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

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

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

最佳答案

如果您的应用程序以 headless 方式运行,则必须将数据设置到显示设备(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 没有电池来保存当前时间。您的主板必须通过互联网同步才能更新其日期/时间。

关于c# - 如何在 Windows Core IoT 应用程序中显示当前时间的时钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34570755/

相关文章:

c# - 是否可以从 IronPython 调用重载的 C# 构造函数?

c# - 空图像中的 SkiaSharp 随机噪声

windows - 在 Windows 8.1 上运行 Windows 10 通用应用程序

c# - 如何从我的应用程序的证书存储中删除证书?

postgresql - 在 CLI 而不是 nano 中显示 psql 查询的结果集

php - 每 30 秒运行一次 PHP 脚本的最佳方法是什么?

c# - Ajax 自动完成扩展程序在母版页中不起作用

c# - 线性函数 : y = mx + b (2 points given) in code

c# - 如何在 UWP 中将数据绑定(bind)到行格式中的 GridView ?

php - 使用php将数据插入数据库中没有填充APACHE2的行