本文介绍了Windows Phone 8.1 后台任务:调试时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发我的后台任务,它只是更新 Windows Phone 中磁贴上的徽章.

I'm trying to develop I background task, that simply updates a badge on a tile in Windows Phone.

我认为我正确地实现了一切,但是当我在调试模式下启动后台任务时,应用程序只是崩溃了.

I think I implemented everything correctly, but when I fire of the back ground task in debug mode, the app simply crashes.

这是我的代码:

背景类

 public sealed class TileBadgeUpdate : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
        updateBadge();
        deferral.Complete();
    }

    private void updateBadge()
    {
        var badgeXML = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber);
        var badge = badgeXML.SelectSingleNode("/badge") as XmlElement;
        badge.SetAttribute("value", "20");
        var badgeNotification = new BadgeNotification(badgeXML);
        BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeNotification);
    }

我在其中一个页面的OnNavigatedTo"中注册了后台任务.我可以成功调试这段代码:

I register the background task in the "OnNavigatedTo" of one of my pages. I can successfully debug this code:

        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            task.Value.Unregister(true);
        }

        var builder = new BackgroundTaskBuilder();
        builder.Name = "NewBGTask";
        builder.TaskEntryPoint = "POCTimesheetEntry.TileBadgeUpdate";
        builder.SetTrigger(new TimeTrigger(15, false));
        var ret = builder.Register();

在 AppxManifest 中

In the AppxManifest

我已经注册了后台任务:

I have registered the background task:

我做错了什么?

提前致谢

马修

推荐答案

  1. 检查您是否单独创建了 BackgroundTask(Windows 运行时组件)项目.当您在 WINDOWS PHONE 8.1 应用程序中创建后台任务时,您必须将单独的项目创建为 WINDOWS 组件

看看您是否忘记将背景任务项目引用添加到您的项目.

See if you have forgot to add reference of Backgroud Task Project to your Project.

就我而言,我忘记添加后台任务项目的引用,因此没有调用 geofencetask.Completed.

In my case I forgot to add reference of background task project hence geofencetask.Completed was not getting called.

这篇关于Windows Phone 8.1 后台任务:调试时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:20