0

我正在为 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";

        }



    }
}
4

2 回答 2

1

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

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

于 2013-08-11T15:01:33.560 回答
0

首先,您将时间跨度初始化为 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-08-11T14:59:30.113 回答