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