制作时钟 UWP (C#) [英] Make Clock UWP (C#)

查看:31
本文介绍了制作时钟 UWP (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Windows 10 编写应用程序,需要在 UI 中显示时间.

I'm writing application for windows 10 and need to display Time inside the UI.

我做了这样的显示

 Time.Text = DateTime.Now.ToString("h:mm:ss tt");

但我需要更新它,关于如何做到这一点有什么建议吗?

But I need to update it, any advice on how I can do this?

推荐答案

在您的 XAML 中试试这个:

Try this inside your XAML:

    <TextBlock x:Name="Time" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>

这是你的代码:

public sealed partial class ClockPage : Page
{
    DispatcherTimer Timer = new DispatcherTimer();

    public ClockPage()
    {
        InitializeComponent();
        DataContext = this;
        Timer.Tick += Timer_Tick;
        Timer.Interval = new TimeSpan(0, 0, 1);
        Timer.Start();
    }

    private void Timer_Tick(object sender, object e)
    {
        Time.Text = DateTime.Now.ToString("h:mm:ss tt");
    }
}

显然,您需要更改类的名称以匹配您所拥有的名称,但是您明白了.您可能想通过使用 MVVM 来整理东西,这只是一个简单的例子.

Obviously you'll need to change the name of the class to match what you've got, but you get the idea. You may want to tidy things up by using MVVM, this is just a bare-bones example.

这篇关于制作时钟 UWP (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆