重启倒计时器

时间:2014-03-09 22:24:55

标签: c# windows-phone-7

我为Windows Phone 7.1做了一个倒数计时器,我有一点问题。我想在调用方法时重新启动计时器

这是计时器的代码

DispatcherTimer timer1 = new DispatcherTimer();
    int tik = 60;
    int min = 1;
    int number;


    void timer1_Tick(Object sender, EventArgs e)
    {


        if (tik < 10)
        {
            myTextBlock.Text = "0" + min.ToString() + ":" + "0" + tik.ToString();
            myTextBlock.FontFamily = new FontFamily("/Fonts/digital-7.ttf#Digital-7");
        }
        else
            if (tik == 60)
            {

                myTextBlock.Text = "0" + min.ToString() + ":" + "00";
                myTextBlock.FontFamily = new FontFamily("/Fonts/digital-7.ttf#Digital-7");

            }
            else
            {
                myTextBlock.Text = "0" + min.ToString() + ":" + tik.ToString();
                myTextBlock.FontFamily = new FontFamily("/Fonts/digital-7.ttf#Digital-7");

            }
        if (tik > 0)
        {
            tik--;
            if (min > 0)
                min--;

        }
        else

            NavigationService.GoBack();
    }
    public void StartTimer(object sender, RoutedEventArgs e)
    {

        timer1.Interval = new TimeSpan(0, 0, 0, 1);
        timer1.Tick += new EventHandler(timer1_Tick);

        timer1.Start(); 


    }

我想重新启动调用此方法的计时器

private void buttonStart_Click(object sender, RoutedEventArgs e)
    {
        string value = textBox3.Text;
        int intval = int.Parse(value);
        Random steps = new Random();
        int n = steps.Next(10, 20);

        if (intval == number)
        { 
            textBox3.Text = "";
            Random rnd = new Random();
            number = rnd.Next(1000, 9999);
            mata.Text = number.ToString();
            n--;


            //here i want to restart the timer



        }
        else
            mata.Text = mata.Text + " " + "NO";

        if (n == 0)
            NavigationService.GoBack();

    }

我尝试使用timer1.stop()然后使用timer1.start(),但它不起作用。

1 个答案:

答案 0 :(得分:1)

由于计时器仍然在滴答作响,我想你想要的只是重置你的变量:

private void buttonStart_Click(object sender, RoutedEventArgs e)
{
    string value = textBox3.Text;
    int intval = int.Parse(value);
    Random steps = new Random();
    int n = steps.Next(10, 20);

    if (intval == number)
    { 
        textBox3.Text = "";
        Random rnd = new Random();
        number = rnd.Next(1000, 9999);
        mata.Text = number.ToString();
        n--;

        tick = 60;
        min = 1;
    }
    else
        mata.Text = mata.Text + " " + "NO";

    if (n == 0)
        NavigationService.GoBack();
}