![](/img/trans.png)
[英]Does System.Timers.Timer.Stop() restart the interval countdown?
[英]Restart a countdown timer
我為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(),但是它不起作用。
由於計時器仍在計時,所以我想您要重置的變量是:
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.