[英]Windows Phone: How to keep timer running after exiting an application?
I want to write an app to show a countdown Timer. 我想编写一个显示倒数计时器的应用程序。 When I exit the application by pressing Home or search buttons timer will stop till I go back to the application. 当我按Home或搜索按钮退出应用程序时,计时器将停止,直到我返回到该应用程序为止。 Here is my Code: 这是我的代码:
public Page1()
{
InitializeComponent();
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//Till timer is greater than 0 show timer.
}
How can I use the timer in a way that it keeps counting (running) even when I exited the application? 如何在退出应用程序时以持续计数(运行)的方式使用计时器?
Save the time when you started the timer in the ApplicationSettings
. 在ApplicationSettings
保存启动计时器的时间。 When the app resumes (activated or re-launched), check the time diff and either restart the timer from the remaining time or show the timer as stopped. 当应用恢复(激活或重新启动)后,请检查时间差异,然后从剩余时间重新启动计时器或将计时器显示为已停止。
在Windows服务中嵌入WCF或Web API服务(Windows服务计时器示例) ,使用此WCF / Web API服务,您可以控制行为而无需重新启动服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.