當您在應用程序外導航時,定時器將不會繼續,但當您導航到應用程序中的另一個頁面時,定時器將繼續。您可以阻止它這樣說:
System.Windows.Threading.DispatcherTimer dt;
public MainPage()
{
InitializeComponent();
dt = new System.Windows.Threading.DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 1000 Milliseconds
dt.Tick += new EventHandler(dt_Tick);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
dt.Stop();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
dt.Start();
}
void dt_Tick(object sender, EventArgs e)
{
listBox1.Items.Add(listBox1.Items.Count + 1); // for testing
}
private void PageTitle_Tap(object sender, GestureEventArgs e)
{
NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative)); // for testing
}
此外,如果你只是檢查大部分時間沒有改變,請考慮使用push notifications數據。
來源
2012-01-12 23:51:29
Pol