【问题标题】:How to properly use Timespan() constructor in C#如何在 C# 中正确使用 Timespan() 构造函数
【发布时间】:2013-08-11 14:53:58
【问题描述】:

我正在为 Windows Phone 8 开发秒表,但实际上我无法正确获取计时。我想每 1000 毫秒更新一次,但由于我是 C# 新手,所以我无法正确处理。这是我现在拥有的代码。 :/

using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Milli_Stopwatch;
using System.Windows.Threading;


namespace Milli_Stopwatch
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor

        DispatcherTimer timer = new DispatcherTimer();

        int millisec = 00;
        int sec = 00;
        int min = 00;
        int hour = 00;

        Boolean check = false;
        public MainPage()
        {
            InitializeComponent();

            timer.Interval = new TimeSpan(0,0,0,0,1);
            timer.Tick += new EventHandler(DispatcherTimer_tick);

        }

        private void DispatcherTimer_tick(object sender, EventArgs e) 
        {
            if (millisec == 1000) 
                { 
                    millisec = 00; 
                    sec = sec + 1; 
                    Secblock.Text = sec.ToString(); 
            }

            if (sec == 59)
            {
                sec = 00;
                min = min + 1;
                Minblock.Text = min.ToString();
            }

            if (min == 59)
            {
                min = 00;
                hour = hour + 1;
                Hourblock.Text = hour.ToString();
            } 

            millisec = millisec + 1;
            Millisecblock.Text = millisec.ToString();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (check == false)
            {
                timer.Start();
                check = true;
                startbutton.Content = "STOP";
            }
            else {
                timer.Stop();
                check = false;
                startbutton.Content = "START";
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            timer.Stop();
            check = false;
            startbutton.Content = "START";
            millisec = 000;
            sec = 00;
            min = 00;
            hour = 00;

            Millisecblock.Text = "00";
            Secblock.Text = "00";
            Minblock.Text = "00";
            Hourblock.Text = "00";

        }



    }
}

【问题讨论】:

  • 不用写一个,你这里有一个codeproject.com/Articles/328522/…
  • @HansPassant 我同意你的观点,我会纠正这个问题,但 DispatcherTimer_tick 没有正确更新秒。毫秒达到 1000 几乎需要 10 秒,然后更新秒。我该怎么办?

标签: c# windows windows-phone-8


【解决方案1】:

当秒数达到 60 而不是 59 时,时钟会转到下一分钟。分钟到小时也是如此。您在几毫秒内得到了它的正确性,但随后通过递增它搞砸了。

无论如何你都不应该这样做,计时器不够准确,你总是会落后。开始时存储 DateTime.UtcNow 的值。当计时器滴答作响时,从 DateTime.UtcNow 中减去它。现在您有了一个准确的 TimeSpan,它始终与经过的挂钟时间相匹配,并且不受计时器准确性的影响。

【讨论】:

    【解决方案2】:

    首先,您将时间跨度初始化为 1 毫秒。恕我直言,使用 TimeSpan.FromMilliseconds(1); 而不是构造函数要容易得多。

    其次,您不能依赖计时器经过的事件在您安排它们发生时准确地发生,因此要使您的秒表正确,您需要使用类似 DateTime.Now - {the DateTime.Now from when you started the timer} 的东西并对其进行解析。

    DateTime startedTimeStamp = DateTime.Now;
    

    然后当你处理经过的事件时。

    txtTime.Text = (DateTime.Now - startedTimeStamp).ToString("G");
    

    无需处理小时、分钟、秒、毫秒等的每个场景......或任何一个。框架会为你处理。

    【讨论】:

      猜你喜欢
      • 2013-02-07
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多