user2823955
user2823955

Reputation: 66

Count down timer to stop counting C# Windows Phone

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 = "";
        }
   }

Everything is working fine, except the fact that the timer keeps on going. After I leave the page, the timer keeps on counting. I have code on another page that re-routes it back to this page. The count variable then resets to 15 and counts down, but since the timer is based on the countDownTimer the whole 15 seconds does not go down. I have found countDownTimer.Stop(), but I am not sure where to place it. I'm a beginner on windows phone. I know this is an easy problem, but I can't figure it out.

Upvotes: 1

Views: 1039

Answers (1)

Mahender
Mahender

Reputation: 5664

You can override the OnNavigatingFrom method on mainpage where you can stop the Timer.

Upvotes: 1

Related Questions