在我的Windows Phone 8應用程序中,我必須執行任務(LiveTiles和ToastNotifications)。我想將這些任務作爲定期的後臺任務來運行。 Toast Notification任務每天運行一次,LiveTiles任務每10分鐘運行一次。在添加第二個定期任務時,它顯示一個錯誤(BNS錯誤:此類型的ScheduledAction的最大數量已被添加)。如果您有解決方案,任何人都可以讓我知道答案。在這裏我附上了代碼。如何在wp8應用程序中定期運行兩個後臺任務?
App.xaml.cs:
var LiveTilesName = "LiveTiles";
var ToastNotificationsName = "ToastNotifications";
PeriodicTask LiveTilesPeriodicTask = ScheduledActionService.Find(LiveTilesName) as PeriodicTask;
PeriodicTask ToastNotificationPeriodicTask = ScheduledActionService.Find(ToastNotificationsName) as PeriodicTask;
if (LiveTilesPeriodicTask != null)
ScheduledActionService.Remove(LiveTilesName);
if (ToastNotificationPeriodicTask != null)
ScheduledActionService.Remove(ToastNotificationsName);
LiveTilesPeriodicTask = new PeriodicTask(LiveTilesName) { Description = "Update Live Tiles." };
ToastNotificationPeriodicTask = new PeriodicTask(ToastNotificationsName) { Description = "Toast Notifications" };
try
{
ScheduledActionService.Add(LiveTilesPeriodicTask);
ScheduledActionService.LaunchForTest(LiveTilesName, TimeSpan.FromSeconds(10));
ScheduledActionService.Add(ToastNotificationPeriodicTask);
ScheduledActionService.LaunchForTest(ToastNotificationsName, TimeSpan.FromSeconds(10));
}
catch (InvalidOperationException e) { }
SchedulerAgentTask代碼:
protected override void OnInvoke(ScheduledTask task)
{
//ScheduledActionService.LaunchForTest("ToastNotifications", TimeSpan.FromSeconds(30));
if (task.Name == "ToastNotifications")
{
SendNotifications();
}
else if(task.Name == "LiveTiles")
{
UpdateTiles();
ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(30));
}
else{}
NotifyComplete();
}
請勿使用LaunchForTest發佈版本應用程序:http://blog.mjfnet.com/2013/01/10/windows-phone-dont-call-launchfortest-in-release/ – MikimotoH