在 WPF 窗口中显示时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37090855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:48:00  来源:igfitidea点击:

Displaying the time in a WPF Window

c#.netwpf

提问by Aravind Suresh

I'm creating a WPF app and I want a label called SystemTimeLabelto display the time. The time, as in, the time. I want it to display the present time, not just the time by which it was loaded. (so, like, you can see the digital clock tick.) I think the whilestatement would do the trick, but I realized that it wouldn't. Any ideas?

我正在创建一个 WPF 应用程序,我想要一个标签SystemTimeLabel来显示时间。时间,就像时间一样。我希望它显示当前时间,而不仅仅是加载时间。(所以,就像,你可以看到数字时钟的滴答声。)我认为这个while语句会起作用,但我意识到它不会。有任何想法吗?

采纳答案by Jon Skeet

Sure - you just need to update the label periodically, which can easily be done with a DispatcherTimer. Just register an event to fire periodically - say 10 times per second - and update the label with the system time every time the event fires.

当然 - 您只需要定期更新标签,这可以通过DispatcherTimer. 只需注册一个事件以定期触发 - 比如说每秒 10 次 - 并在每次事件触发时使用系统时间更新标签。

(Note that you don't want to do this only once per second, even if you'll only actually changethe text once per second, or you could end up with odd effects where it seems to pause for two seconds occasionally due to the timer not firing exactlyonce per second.)

(请注意,即使您实际上每秒只更改一次文本,您也不希望每秒只执行一次此操作,否则最终可能会出现奇怪的效果,由于计时器不费一枪正好每秒一次。)

It's possiblethat this could be done with WPF animations instead somehow, but a timer feels like a more natural solution.

这是可能的,这可能与WPF动画来完成,而不是莫名其妙,但计时器感觉更自然的解决方案。

回答by Drunken Code Monkey

If you want to follow the MVVM pattern, the main problem with a datetime is that it doesn't implement INotifyPropertyChanged. So while you can bind a datetime property to your label, it won't update automatically. So, you just need to implement update notifications for your datetime property in your viewmodel. So, create a public property with change notification, and start a timer to update that property every half second or so. Then bind your label's content to that property.

如果你想遵循 MVVM 模式,日期时间的主要问题是它没有实现 INotifyPropertyChanged。因此,虽然您可以将日期时间属性绑定到标签,但它不会自动更新。因此,您只需要在视图模型中为日期时间属性实现更新通知。因此,创建一个带有更改通知的公共属性,并启动一个计时器以每半秒左右更新该属性。然后将您的标签内容绑定到该属性。

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private DateTime _now;

    public MyViewModel()
    {
        _now = DateTime.Now;
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(500);
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }

    public DateTime CurrentDateTime
    {
        get {return _now;}
        private set
        {
            _now = value;
            PropertyChanged(this, new PropertyChangedEventArgs("CurrentDateTime"));
        }
    }

    void timer_Tick(object sender, EventArgs e)
    {
        CurrentDateTime = DateTime.Now;
    }

}

<Label x:Name="SomeLabel" Content="{Binding CurrentDateTime}" ContentStringFormat="yyyy-MM-dd HH:mm:ss" />