1
votes

I have a Windows Phone 8 app that I recently upgraded to 8.1 Silverlight. I'd like to use the new tile templates. Right now I have a ScheduledTaskAgent that uses ShellTile.

In order to use the new live tiles I changed the notification service to WNS in my WMAppManifest.xml. I removed the code to register the old background task and added this code instead:

var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
    backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
{
    foreach (var task in BackgroundTaskRegistration.AllTasks)
    {
        if (task.Value.Name == "LiveTileBackgroundTask")
        {
            task.Value.Unregister(true);
        }
    }

    BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
    taskBuilder.Name = "LiveTileBackgroundTask";
    taskBuilder.TaskEntryPoint = "BackgroundTasks.LiveTileBackgroundTask";
    taskBuilder.SetTrigger(new TimeTrigger(15, false));
    var registration = taskBuilder.Register();
}

I created a Windows Phone 8.1 Windows Runtime Component called BackgroundTasks that contains a BackgroundTask called LiveTileBackgroundTask:

public sealed class LiveTileBackgroundTask : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

        const string xml = "<tile>"
                           + "<visual>"
                           +  "<binding template='TileWideText01'>"
                           +   "<text id='1'>Text Field 1 (larger text)</text>"
                           +   "<text id='2'>Text Field 2</text>"
                           +   "<text id='3'>Text Field 3</text>"
                           +   "<text id='4'>Text Field 4</text>"
                           +   "<text id='5'>Text Field 5</text>"
                           +  "</binding>  "
                           + "</visual>"
                           +"</tile>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);

        TileNotification tileNotification = new TileNotification(doc);
        TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);

        deferral.Complete();
    }
}

I added a reference to this assembly in my Windows Phone project.

I also added a Background task declaration in my Package.appxmanifest that has BackgroundTasks.LiveTileBackgroundTask as an Entry point. I selected Timer and System event as supported task types.

When I run the app though, nothing happens. No live tile appears. I ran through the background task and everything goes well without any exceptions.

2
Hi, did you try just updating the tile in the foreground instead of in a background task? The reason that I asked is because I can't even get the tile to update in the foreground...Justin XL
Hi Xin, I tried that and it didn't work either.Leon Cullens
Please take a look at this one stackoverflow.com/questions/23589479/…Justin XL
@Xin thanks, I did set the notification service to WNS. Could you pastebin your manifest?Leon Cullens

2 Answers

3
votes

You say "No live tile appears". The code you've posted does not create a live tile - it just updates one. You have to manually pin it - the primary tile cannot be pinned through code.

If that's not the problem, maybe you're not looking at the wide tile? This template is for a wide tile, so the square tile won't be updated by this. I'd suggest using the NotificationsExtensions library. It was originally for Windows Store apps, but I think it would work for WP as well. (I've used it, but just for a test, not for real, so there may be issues.) It allows you to easily specify the template and params for both wide and square tiles.

And finally, to have a wide tile, you have to manually edit the Package.appxmanifest file. You must add the Wide310x150Logo attribute to the DefaultTile element.

That's all I can think of. Hope it helps.

1
votes

Continuous background execution is not supported for Silverlight 8.1 apps

Windows Phone 8 apps can continue to run in the background after the user navigates away from the app under certain conditions. This feature is not available for Silverlight 8.1 apps. If you need this feature, you should continue to use a Windows Phone 8 app. For more information, see Running location-tracking apps in the background for Windows Phone 8.

Platform compatibility and breaking changes for Windows Phone Silverlight 8.1 apps

Windows Phone 8.1 Windows Runtime Component can only be used with Windows Phone 8.1 Runtime(Store) app