当我添加条件或尝试在构造函数之外启动计时器时,DispatcherTimer 没有执行代码。
我遇到的问题是,如果我在没有条件的情况下在构造函数中启动 DispatchTimer,我的图形层会在单击按钮 Show_ClickStuff 之前添加。如果我没有在构造函数中启动所有内容,并且我将 if 条件放在 Show_ClickStuff 或 _timer_tick 中,我可以在代码上放置断点并观察计时器命中代码行但不会执行代码行。如果我删除条件代码将执行。我尝试了各种变化,但无法按照我需要的方式工作
使用 System.Windows.Threading;.NET Framework 4.5 代码文件:.xaml.cs Silverlight 4
public Tool()
{
InitializeComponent();
_timer1 = new DispatcherTimer();
//_timer1.Start();
_timer1.Interval = TimeSpan.FromMilliseconds(4000);
_timer1.Tick += new EventHandler(_timer_tick);
}
private void Show_ClickStuff(object sender, RoutedEventArgs e)
{
ToggleStuff();
if (showStuff.IsChecked == true)
{
//Timer_Toggle(this, null);
//_timer1 = new DispatcherTimer();
//_timer1.Start();
//_timer1.Interval = TimeSpan.FromMilliseconds(4000);
//_timer1.Tick += new EventHandler(_timer_tick);
_timer1.Start();
}
else if (!_timer1.IsEnabled && showStuff.IsChecked == true)
{
_timer1.Start();
}
else
{
_timer1.Stop();
}
}
private void _timer_tick(object sender, EventArgs e)
{
_map.Layers.Remove(_stuffLayer);
GetStuff();
_map.Layers.Add(_stuffLayer);
}