public partial class gamePage : PhoneApplicationPage
{
DispatcherTimer countDownTimer;
public gamePage()
{
InitializeComponent();
countDownTimer = new DispatcherTimer();
countDownTimer.Interval = new TimeSpan(0, 0, 0, 1);
countDownTimer.Tick += new EventHandler(countDownTimerEvent);
countDownTimer.Start();
txtHit.Text = "0";
txtCountdown.Text = "" + "seconds remaining";
}
int buttonCount = 0;
string stringButtonCount = "";
Random rnd = new Random();
int count = 15;
void countDownTimerEvent(object sender, EventArgs e)
{
txtCountdown.Text = count + " Seconds Remaining";
if (count > 0)
{
count--;
}
if (count == 0)
{
NavigationService.Navigate(new Uri("/highScore.xaml", UriKind.Relative));
count = 15;
buttonCount = 0;
stringButtonCount = "";
}
}
一切正常,除了计时器继续运行。离开页面后,计时器继续计数。我在另一个页面上有代码,可以将其重新路由回该页面。count 变量然后重置为 15 并倒计时,但由于计时器基于 countDownTimer,因此整个 15 秒不会倒计时。我找到了 countDownTimer.Stop(),但我不确定将它放在哪里。我是Windows Phone的初学者。我知道这是一个简单的问题,但我无法弄清楚。