我有Windows Phone Silverlight 8.1的后台任务,只要我不需要访问互联网就可以正常工作。
以下是我尝试运行的代码:
var query = from photoTest2 in ParseObject.GetQuery("PhotoTest2")
where photoTest2.Get<string>("username") != "x"
select photoTest2;
int resultsCount = await query.OrderByDescending("createdAt").WhereWithinDistance("location", point, ParseGeoDistance.FromKilometers(10)).CountAsync();
当我使用标准方法并在MessageBox中显示“resultCount”时,此方法运行正常,并且只需不到一秒钟。但是,作为后台任务,它只是无限期地停止。
这是后台任务的代码:
public sealed class FirstTask : IBackgroundTask
{
int resultsCount;
public async void Run(IBackgroundTaskInstance taskInstance)
{
DateTime timeNow = DateTime.Now;
TimeSpan minus28 = new TimeSpan(0, 28, 0);
DateTime timeGone = timeNow.Subtract(minus28);
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
await GetCount();
deferral.Complete();
ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList textElements = toastXml.GetElementsByTagName("text");
textElements[0].AppendChild(toastXml.CreateTextNode("New Pictures!"));
textElements[1].AppendChild(toastXml.CreateTextNode(resultsCount + " new images in your area"));
ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
}
private async Task GetCount()
{
var point = new ParseGeoPoint(11.11, -11.11);
var distance = new ParseGeoDistance(360);
var query = from photoTest2 in ParseObject.GetQuery("PhotoTest2")
where photoTest2.Get<string>("username") != "x"
select photoTest2;
resultsCount = await query.OrderByDescending("createdAt").WhereWithinDistance("location", point, ParseGeoDistance.FromKilometers(10)).CountAsync();
}
非常感谢帮助:)