在Win8应用中计时器计数

时间:2012-05-08 09:56:40

标签: c# xaml windows-8 windows-runtime winrt-xaml

我正在构建一个win8应用程序,它有3个按钮(Button1,Button2和StartButton)和计时器。 Button1和Button2被禁用。如果单击StartButton,则启用Button1并计算20秒内的点击次数并显示在textblock1中。计时器结束后,Button1被禁用,Button2被启用,点击次数被计算并显​​示在textblock2中。我的问题是计时器为Button1正确滴答而不是Button2。启用button2后,计时器变得更快。有人能帮我吗? 我的代码如下:

    private int count1=0;
    private int count2=0;
    private int clickCounter = 0;
    private int timeLeft;
    private DispatcherTimer timer;

    private void StartTimer()
    {
        if (this.timer != null)
        {
            this.StopTimer();
        }
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0,0,0,1);
        timer.Tick += timer_Tick;
        timer.Start();

    }

    private void StopTimer()
    {
        if (this.timer != null)
        {
            this.timer.Stop();
            this.timer = null;
        }
    }

    public void timer_Tick(object sender, object args)
    {

        if (timeLeft > 0)
        {
            timeLeft = timeLeft - 1;
            timerTextBlock.Text = Convert.ToString(timeLeft);
            this.StartButton.IsEnabled = false;
        }
        else
        {
            StopTimer();
            if (clickCounter==2)
            {
                ShowResult();
                this.Button2.IsEnabled = false;
                this.StartButton.IsEnabled = false;
            }
            else
            {
                myMsg.Text = "Time's up!";
                this.Button1.IsEnabled = false;
                this.StartButton.IsEnabled = true;
            }
        }
    }

    private void Button1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        // TODO: Add event handler implementation here.
        count1++;
        this.textblock1.Text=count1.ToString();

    }

    private void Button2_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        count2++;
        this.textblock2.Text=count2.ToString();
    }
    public void ResetTimer()
    {
            timeLeft = 20;
    }
    private void StartButton_Click(object sender, RoutedEventArgs e)
    {
        clickCounter++;
        if (textblock1.Text == "0")
        {
            ResetTimer();
            StartTimer();
            this.Button1.IsEnabled = true;
        }
        else
        {
            ResetTimer();
            StartTimer();
            this.Button2.IsEnabled = true;
        }
    }

1 个答案:

答案 0 :(得分:3)

每次调用StartTimer方法timer.Tick += timer_Tick;时都会执行。这意味着如果您第二次调用StartTimer,每个tick都会调用两个事件。将您的代码拆分为:

private void InitTimer()
{
    timer = new DispatcherTimer();
    timer.Interval = new TimeSpan(0,0,0,1);
    timer.Tick += timer_Tick;
}

private void StartTimer()
{
    timer.Start();
}

并仅调用InitTimer。另一个选项是使用代码if (this.timer != null)timer.Tick -= timer_Tick;中取消注册您的活动 我看到的第二点是命名冲突:你的StartTimer方法中有一个私有全局变量计时器和一个变量计时器。

//编辑:完整更新的代码:

private int count1=0;
private int count2=0;
private int clickCounter = 0;
private int timeLeft;
private DispatcherTimer timer;

private void StartTimer() {
    if (timer == null) {
        timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0,0,0,1);
        timer.Tick += timer_Tick;
    }
    timer.Stop();
    timeLeft = 20;
    timer.Start();
}

public void timer_Tick(object sender, object args) {
    if (timeLeft > 0) {
        timeLeft = timeLeft - 1;
        timerTextBlock.Text = Convert.ToString(timeLeft);
    } else {
        timer.Stop();
        if (clickCounter==2) {
            ShowResult();
            Button2.IsEnabled = false;
            StartButton.IsEnabled = false;
        } else {
            myMsg.Text = "Time's up!";
            Button1.IsEnabled = false;
            StartButton.IsEnabled = true;
        }
    }
}

private void Button1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
    count1++;
    textblock1.Text=count1.ToString();
}

private void Button2_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
    count2++;
    textblock2.Text=count2.ToString();
}

private void StartButton_Click(object sender, RoutedEventArgs e) {
    clickCounter++;
    StartButton.IsEnabled = false;
    if (textblock1.Text == "0"){
        Button1.IsEnabled = true;
        StartTimer();
    } else {
        Button2.IsEnabled = true;
        StartTimer();
    }
}