C# 如何在C中正确使用Timespan()构造函数#

C# 如何在C中正确使用Timespan()构造函数#,c#,windows,windows-phone-8,C#,Windows,Windows Phone 8,我正在为WindowsPhone8开发秒表,但实际上我无法正确地获取计时信息。我想每1000毫秒更新一次第二次,但由于我是C#新手,所以无法正确地进行更新。这是我现在掌握的代码:/ using Microsoft.Phone.Controls; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows

我正在为WindowsPhone8开发秒表,但实际上我无法正确地获取计时信息。我想每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";

        }



    }
}

首先,您正在将timespan初始化为1毫秒。IMHO,只需使用
timespan.fromMillistics(1)就容易多了而不是构造函数

其次,您不能依赖计时器经过的事件在您计划它们发生时准确发生,因此要使秒表正确,您需要使用类似于
DateTime.Now-{DateTime.Now的内容,从启动计时器时开始}
并对其进行解析

DateTime startedTimeStamp = DateTime.Now;
然后当您处理经过的事件时

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

无需在小时、分钟、秒、毫秒等时间内处理每个场景。。。或者任何一种。框架将为您处理此问题。

时钟将在第二分钟达到60而不是59时进入下一分钟。分钟到小时都一样。你在几毫秒内都做得不错,但后来却把它累加了,搞砸了


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

无需编写时间跨度,您在这里有一个@HansPassant我同意您的意见,我将纠正此问题,但Dispatcher_tick没有正确更新秒。毫秒达到1000秒几乎需要10秒,然后秒被更新。我该怎么办?