今天在爆UWP的定时后台任务,坑有点多。有的坑在很多网上的文章里都没提到,非常的坑。刚刚开荒成功了,把经验写出来分享: 1. 写一个后台任务的类,继承IBackgroundTask接口 通常,在设计应用程序结构的时候,我们会建类库项目(Class Library)放这些类。比如 FarkBackgroundTask.Core 因为是UWP工程,所以建的类库也要是Universal Windows的。注意,这里我们已经埋下了一个巨坑,稍后会解释。 我们的类代码如下: public class SayFarkTask : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) { Debug.Write("================ Fark the
Recently I am rewriting an old App using WinRT, I need to display a clock, but I find there is no Timer control.
It looks like I have to implement the Timer myself. I don't use Thread.Sleep because it will block UI thread. I prefer using async await over it. To replace Thread.Sleep, I use Task.Delay:
while (true)
{
// 要做的操作
await Task.Delay(毫秒);
}
为了增加逼格和可重用性,我们需要进一步封装。注意观察本高(