dispatcherTimer无法在WPF中工作

时间:2013-11-29 21:07:10

标签: c# wpf

我有使用WPF和C#的事件监听器。我所做的只是改变某些按钮的颜色。我改变了一个的颜色,然后我继续下一个。问题是我正在尝试使它具有交互性,并根据我在XAML中使用的滑块的值,使用不同的时间更改颜色。问题是,当我重新启动游戏超过3次时,计时器开始忽略滑块的值,并且按钮的颜色会改变为与滑块上的什么不匹配的模式。 有什么建议吗?

private void Restart_Click(object sender, RoutedEventArgs e)
    {

        SnackBoxes = 2;
        theBabies[0] = new Baby();
        theBabies[1] = new Baby();
        theBabies[2] = new Baby();
        theBabies[3] = new Baby();
        theBabies[4] = new Baby();
        theBabies[5] = new Baby();
        theBabies[6] = new Baby();
        theBabies[7] = new Baby();
        theBabies[8] = new Baby();
        theBabies[9] = new Baby();
        resetBabies();
        dispatcherTimer1 = new DispatcherTimer();
        int timeSlider = (int)(11 - theSlider.Value);
        MessageBox.Show(""+timeSlider);
        dispatcherTimer1.Interval = TimeSpan.FromSeconds(timeSlider);
        dispatcherTimer1.Tick += delegate
        {
            int nextBabyRed = nextBabyToTurnRed(theBabies);

            switch (nextBabyRed)
            {
                case 0:
                    Baby1.Background = Brushes.Red;
                    theBabies[0].Happy = false;
                    break;
                case 1:
                    Baby2.Background = Brushes.Red;
                    theBabies[1].Happy = false;
                    break;
                case 2:
                    Baby3.Background = Brushes.Red;
                    theBabies[2].Happy = false;
                    break;
                case 3:
                    Baby4.Background = Brushes.Red;
                    theBabies[3].Happy = false;
                    break;
                case 4:
                    Baby5.Background = Brushes.Red;
                    theBabies[4].Happy = false;
                    break;
                case 5:
                    Baby6.Background = Brushes.Red;
                    theBabies[5].Happy = false;
                    break;
                case 6:
                    Baby7.Background = Brushes.Red;
                    theBabies[6].Happy = false;
                    break;
                case 7:
                    Baby8.Background = Brushes.Red;
                    theBabies[7].Happy = false;
                    break;
                case 8:
                    Baby9.Background = Brushes.Red;
                    theBabies[8].Happy = false;
                    break;
                case 9:
                    Baby10.Background = Brushes.Red;
                    theBabies[9].Happy = false;
                    break;
                default:
                    Console.WriteLine("Default case");
                    break;
            }

            //dispatcherTimer1.Stop();
        };
        dispatcherTimer1.Start();
        //MessageBox.Show(""+theSlider.Value);
    }
}

2 个答案:

答案 0 :(得分:1)

不要随时创建新的计时器,只需更改间隔。

所以而不是:

    dispatcherTimer1 = new DispatcherTimer();
    int timeSlider = (int)(11 - theSlider.Value);
    MessageBox.Show(""+timeSlider);
    dispatcherTimer1.Interval = TimeSpan.FromSeconds(timeSlider);
    dispatcherTimer1.Tick += delegate

在外部范围声明dispatchTimer,并创建但已停止。将委托从Click处理程序移出到自己的方法中:

private void DispatchTimerClick(...)
{
   // ...
}

在最初由表单创建计时器时挂起该事件处理程序。

然后,您的点击处理程序变为:

    int timeSlider = (int)(11 - theSlider.Value);
    MessageBox.Show(""+timeSlider);
    dispatcherTimer1.Interval = TimeSpan.FromSeconds(timeSlider);
    dispatcherTimer1.Start();

除非你以某种方式阻止它(或者它从未启动过),否则Start在这里并不是必需的。

答案 1 :(得分:1)

“dispatcherTimer1 = new DispatcherTimer();”

我认为问题出在哪里。你正在创建新的计时器,但旧计时器仍在运行。

尝试在创建新计时器之前停止旧计时器。