1
votes

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:

Declaration

What am I doing wrong?

Thanks in advance

Matthew

1
Can you navigate to a break point on Run method? Did you create 2 different projects? Did you switch process in Debug Location toolbar?t4nhpt
Hello, I have a breakpoint in the run of my class. It does not reach the breakpoint. I have added a class Library of type 'Windows Runtime Component (Windows Phone)' Picture: i.imgur.com/hyPH0Ew.pngMathieu Vantieghem
I found the solution myself: I had to add a reference to the background task project in the app project. Thanks for your help !Mathieu Vantieghem
Hi, BadgeUpdateManager reference not there in the Windows RunTime component Lib. Right? So how you managed to update the tile from bg task ?asitis

1 Answers

0
votes
  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.