1

我正在构建一个具有 3 个按钮(Button1、Button2 和 StartButton)和计时器的 win8 应用程序。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;
        }
    }
4

1 回答 1

3

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

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();
    }
}
于 2012-05-08T11:18:46.910 回答