Windows phone 7 如何从外部事件停止WP7上的计时器?
我需要一个计时器在5秒后将我重定向到另一个页面,问题是它每5秒将我重定向到这个页面,所以我需要停止它。如果我在tmr.Start之后停止它,它不会执行该事件。在OnTimerTick事件中如何执行此操作 描述 两种可能的解决办法 在类级别而不是在方法中创建Dispatchermer实例。然后可以通过OnTimerTick方法访问它们。 您可以在OnTimerTick方法中将发件人强制转换为Dispatchermer。 样品 一,。解决方案 二,。解决方案 更多信息Windows phone 7 如何从外部事件停止WP7上的计时器?,windows-phone-7,Windows Phone 7,我需要一个计时器在5秒后将我重定向到另一个页面,问题是它每5秒将我重定向到这个页面,所以我需要停止它。如果我在tmr.Start之后停止它,它不会执行该事件。在OnTimerTick事件中如何执行此操作 描述 两种可能的解决办法 在类级别而不是在方法中创建Dispatchermer实例。然后可以通过OnTimerTick方法访问它们。 您可以在OnTimerTick方法中将发件人强制转换为Dispatchermer。 样品 一,。解决方案 二,。解决方案 更多信息 尝试这样构造代码。将使计时器对
尝试这样构造代码。将使计时器对象保持在范围内,以便在第一次滴答声发生后停止它
class SimpleExample
{
DispatcherTimer timer;
public SimpleExample()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(5);
timer.Tick += new EventHandler(OnTimerTick);
}
public void SomeMethod()
{
timer.Start();
}
void OnTimerTick(object sender, EventArgs e)
{
timer.Stop();
NavigationService.Navigate(new Uri("/lvlSet1/s1lvl3.xaml", UriKind.Relative));
}
}
public class YourClass
{
DispatcherTimer tmr = new DispatcherTimer();
public void YourMethodThatStartsTheTimer()
{
tmr.Interval = TimeSpan.FromSeconds(5);
tmr.Tick += new EventHandler(OnTimerTick);
tmr.Start();
}
void OnTimerTick(object sender, EventArgs e)
{
tmr.Stop();
NavigationService.Navigate(new Uri("/lvlSet1/s1lvl3.xaml", UriKind.Relative));
}
}
void OnTimerTick(object sender, EventArgs e)
{
((DispatcherTimer)sender).Stop();
NavigationService.Navigate(new Uri("/lvlSet1/s1lvl3.xaml", UriKind.Relative));
}
class SimpleExample
{
DispatcherTimer timer;
public SimpleExample()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(5);
timer.Tick += new EventHandler(OnTimerTick);
}
public void SomeMethod()
{
timer.Start();
}
void OnTimerTick(object sender, EventArgs e)
{
timer.Stop();
NavigationService.Navigate(new Uri("/lvlSet1/s1lvl3.xaml", UriKind.Relative));
}
}