【问题标题】:How to display a clock with the current time in a Windows Core IoT app?如何在 Windows Core IoT 应用程序中显示当前时间的时钟?
【发布时间】:2016-01-02 21:09:16
【问题描述】:

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    如果您的应用在无头模式下运行,则必须将数据设置到显示设备(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 同步以更新其日期/时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-06
      • 2019-07-13
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多