在我的 Windows Phone 8 应用程序中,我必须执行任务(LiveTiles 和 ToastNotifications)。我想将这些任务作为定期后台任务运行。Toast 通知任务每天运行一次,我的 LiveTiles 任务每 10 分钟运行一次。添加第二个定期任务时,它显示错误(BNS 错误:已添加此类型的最大 ScheduledActions 数量)。如果您有解决方案,任何人都可以告诉我答案。在这里我附上了代码。
应用程序.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) { }
SchedulerAgent任务代码:
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();
}