在 WPF 窗口中显示正在运行的计时器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24922197/
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 12:12:26  来源:igfitidea点击:

Show a running timer in a WPF window

c#.netwpftimer

提问by KField

I am new to WPF and I am working on a project which needs to show a running timer on the window. The page with the timer is showing the detail information of a test, such as ID, test name, status, start time, end time, blahblahblah...

我是 WPF 的新手,我正在开发一个需要在窗口上显示正在运行的计时器的项目。带有计时器的页面显示了测试的详细信息,例如 ID、测试名​​称、状态、开始时间、结束时间、等等……

I really wish that I could have a timer on the page that tells the user that how long the test has been running. But I didn't find a tool in the toolbox which can show a running timer and I don't even know how to put moving stuff on the page...

我真的希望我可以在页面上有一个计时器,告诉用户测试已经运行了多长时间。但是我在工具箱中没有找到可以显示正在运行的计时器的工具,我什至不知道如何在页面上放置移动的东西......

Could you guys tell me how to add a running timer on the page?

你们能告诉我如何在页面上添加一个正在运行的计时器吗?

In addition, if this is possible, I wish my timer could start from some specific time instead of 00:00:00. The reason I need this is because the user can open this page when the test has been running for a while, and the elapsed time shown on the timer should be (current_time - start_time) and start from here.

此外,如果可能的话,我希望我的计时器可以从某个特定时间而不是 00:00:00 开始。我需要这个的原因是因为用户可以在测试运行一段时间后打开这个页面,并且计时器上显示的经过时间应该是 (current_time - start_time) 并从这里开始。

If the test start at: 7:00 AM and the user opens the page at 7:05AM and the test is still running, the timer should start from 00:05:00.

如果测试开始于:上午 7:00 并且用户在上午 7:05 打开页面并且测试仍在运行,则计时器应从 00:05:00 开始。

Thank you so much, guys! Hope this long description won't make you bored:)

十分感谢大家!希望这个冗长的描述不会让你感到无聊:)

回答by Scott Solmer

Here is a very basic example I threw together.

这是我拼凑的一个非常基本的例子。

using System.Windows.Threading;

namespace BasicTimer
{
    public partial class MainWindow : Window
    {
        DispatcherTimer t;
        DateTime start;
        public MainWindow()
        {
            InitializeComponent();
            t = new DispatcherTimer(new TimeSpan(0, 0, 0, 0, 50), DispatcherPriority.Background,
                t_Tick, Dispatcher.CurrentDispatcher); t.IsEnabled = true;
            start = DateTime.Now;
        }

        private void t_Tick(object sender, EventArgs e)
        {
            TimerDisplay.Text = Convert.ToString(DateTime.Now - start);
        }

MainWindow XAML

主窗口 XAML

<Window x:Class="BasicTimer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="200">
    <Grid>
        <TextBlock x:Name="TimerDisplay" HorizontalAlignment="Left"/>
    </Grid>
</Window>