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手机的初学者。我知道这是一个简单的问题,但我无法弄清楚。
答案 0 :(得分:1)
您可以在主页上覆盖 OnNavigatingFrom 方法,您可以在此停止计时器。