Windows Phone 8.1 Background task: crashes when debugging

一曲冷凌霜 提交于 2019-12-24 15:23:14

问题


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.

Here is my code:

The Background class

 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);
    }

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();

In the AppxManifest

I have registered the background task:

What am I doing wrong?

Thanks in advance

Matthew


回答1:


  1. Check if you have created BackgroundTask (Windows Run Time Component) project separately. IT IS MUST WHEN YOU CREATE BACKGROUND TASK IN WINDOWS PHONE 8.1 APP, YOU CREATE SEPARATE PROJECT AS WINDOWS COMPONENT

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

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



来源:https://stackoverflow.com/questions/29765888/windows-phone-8-1-background-task-crashes-when-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!