【发布时间】:2017-02-26 10:22:23
【问题描述】:
我需要检测重启或设备启动,为此我遵循了这个主题 (Detecting reboot programmatically in Windows Phone 8.1),但在我的情况下,取消的方法永远不会在后台任务中调用。
当我开始调试时,在强制更改时区后调用我的方法:
builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
我的后台任务是:
public void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral defferal = taskInstance.GetDeferral();
taskInstance.Canceled += TaskInstance_Canceled;
defferal.Complete();
}
private async void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
BackgroundTaskDeferral defferal = sender.GetDeferral();
try
{
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
await localFolder.CreateFileAsync("bruno.txt", CreationCollisionOption.OpenIfExists);
}
catch (Exception e)
{
Debug.WriteLine("Fail to create File test: " + e);
}
defferal.Complete();
}
我知道它永远不会被调用,因为下一个例程总是为 false(它在应用程序启动 MainPage 方法时起作用):
【问题讨论】:
标签: c# windows-phone-8.1 windows-store-apps background-process windows-store