要实现后台任务,需要实现IBackgroundTask接口
1 public sealed class SimpleTask : IBackgroundTask 2 { 3 public void Run(IBackgroundTaskInstance taskInstance) 4 { 5 // TODO 6 } 7 }
假设需要用到异步方法,则要这么写:
1 public sealed class SimpleAsyncTask : IBackgroundTask 2 { 3 public async void Run(IBackgroundTaskInstance taskInstance) 4 { 5 BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); 6 // TODO 7 deferral.Complete(); 8 } 9 }