0

我正在使用第 3 方 rest api 来查询数据并将其显示在我的应用程序中。我必须在晚上 12 点左右执行一项任务。它将执行后台任务以从 rest api 查询数据并更新动态磁贴并生成通知。我只想将 C# 仅用于此任务。我不知道完成这项任务的最佳方法是什么。但是我使用下面的代码来执行后台任务来做到这一点,这是行不通的。不知道为什么?

BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = taskName;
SystemTrigger trigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
taskBuilder.SetTrigger(trigger);
taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
taskBuilder.TaskEntryPoint = typeof(BackgroundTask.BackgroundTask).FullName;
taskBuilder.Register();

并从后台任务中查询数据并生成 toast 通知。

为什么此代码不起作用或何时触发此任务的任何帮助。有没有更好的方法来完成上述任务?

谢谢

4

3 回答 3

2

Regarding the code you have not working...

For Windows Phone 8.1 unlike Windows 8\8.1, you are required to call BackgroundExecutionManager.RequestAccessAsync() (search MSDN\internet) for ANY background task before registering task(s) whereas in Windows this is only required for some tasks. Make sure your code calls this and validate the returned value before registering your background task(s).

Regarding knowing if your task "worked"...

It's a good idea to have the background task implementation run (IBackgroundTask::Run()) independent of the trigger\conditions you've set to ensure it performs without issue by debugging it. See instructions in the following link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/jj542416.aspx.

Regarding your use of SystemConditionType.InternetAvailable...

I'm not 100% about this but I'm pretty certain this will be redundant given you already have a SystemTriggerType.InternetAvailable. I don't know of a situation where the trigger would fire but the condition wouldn't be true.

Regarding the requirement you've mentioned...

If I understand your requirement correctly you have different options here:

  1. If your app is a Windows Phone XAML app that need to run based on time, I would recommend either TimeTrigger or MaintenanceTrigger triggers (as opposed to the SystemTrigger). These are both Background Tasks. For general info on Background Tasks and links to the TimeTrigger and MaintenanceTrigger documentation see this MSDN link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/hh977056.aspx.
  2. If your app is a Windows Phone Silverlight 8.0 app you can use Background Agents, specifically either PeriodicTask or ResourceIntensiveTask. See the links posted by others or search the MSDN\internet for more info.
  3. If your app is a Windows Phone Silverlight 8.1 app you can use the option in either 1 or 2 above.
于 2014-09-01T19:16:16.997 回答
1

我认为您应该尝试使用PeriodicTask。还要考虑链接中提到的约束。

于 2014-08-22T06:49:32.110 回答
0

创建一个具有输出类型:Windows 运行时组件的类,并将您的类从 IBackroundTask 继承,因此如果您从模拟器中使用该类来启动应用程序,我认为您的注册任务应用程序在模拟器中不活动。

于 2015-04-09T12:26:14.323 回答