Show current time WPF

后端 未结 3 615
醉话见心
醉话见心 2021-01-07 06:45

The only way to show current time updating regularly I found is to use timer. Of course, I can implement INotifyPropertyChanged and some special property to be

3条回答
  •  爱一瞬间的悲伤
    2021-01-07 07:00

    Using Task.Delay can produce an high CPU usage!

    In the XAML code write this:

    Next in the xaml.cs write this:

    [...]
    public MainWindow()
    {
        InitializeComponent();
        DispatcherTimer LiveTime = new DispatcherTimer();
        LiveTime.Interval = TimeSpan.FromSeconds(1);
        LiveTime.Tick += timer_Tick;
        LiveTime.Start();
    }
    
    void timer_Tick(object sender, EventArgs e)
    {
        LiveTimeLabel.Content = DateTime.Now.ToString("HH:mm:ss");
    }
    [...]
    

提交回复
热议问题