我在我的應用程序中有DispatcherTimer
,它計算了我的應用程序的活動時間,並且我想在用戶導航到其他應用程序時調用DispatcherTimer.Stop()
(因此它不再計數) 。然後當用戶返回到我的應用程序時恢復計數。當切換應用程序時停止DispatcherTimer(Windows Store)
如何做到這一點?
我在我的應用程序中有DispatcherTimer
,它計算了我的應用程序的活動時間,並且我想在用戶導航到其他應用程序時調用DispatcherTimer.Stop()
(因此它不再計數) 。然後當用戶返回到我的應用程序時恢復計數。當切換應用程序時停止DispatcherTimer(Windows Store)
如何做到這一點?
在Application類的Suspending事件中,執行DispatcherTimer.Stop()方法。
在Application類的Resuming事件中,恢復DispatcherTimer。
應用程序類的暫停事件將在用戶切換應用程序或僅從應用程序導航時執行。 Resuming事件將在用戶關注應用程序並將其帶到前臺時執行。
參見: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.suspending.aspx http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.resuming.aspx
其實我已經試過這種方式,但是當我在調試模式下運行它並返回到Visual Studio(不停止應用程序的C),它仍然打印時間輸出(所以'DispatcherTimer.Tick'繼續運行...) –
當在調試模式下,你必須強制通過使用位於調試目標下拉菜單中的菜單將應用程序暫停和恢復。這就是爲什麼你沒有看到它暫停或恢復。 –
嗯......我以爲它可能是其中一種可能性...... ^^ 希望它能工作:)謝謝 –