Zenilogix
Zenilogix

Reputation: 1393

Simple UWP live tile update

I have a simple UWP shopping list app which talks to nothing and keeps its data in local storage. When the app is suspended, I write the current list to local storage. What I want to do at this same time (or OnNavigatedFrom the main page) is to also update the live tile to show a count of the number of items currently on the list so that the user can see this fact without having to launch the app.

I am having trouble finding docs/samples which are geared to a simple case like this... no background task, no push notifications, nothing happening on some schedule, just a simple update of a tile essentially in response to a user action. The examples I can find appear to involve the use of a TileUpdater, but I don't understand the need for one, or if/how it should be used in this simple case.

What is the bare minimum sequence of steps I have to do in my code to update say, a badge count (both to put a value there and to remove if the count would be zero)?

Upvotes: 1

Views: 414

Answers (1)

Zenilogix
Zenilogix

Reputation: 1393

Found my own answer.

I created a service class into which I added the following method which I call whenever I need to update the badge count:

using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;

    /// <summary>
    /// Add a badge count to the application tile (or remove if the count is zero)
    /// </summary>
    /// <param name="count">Number of items</param>
    public static void Update(int count)
    {
        // Get an XmlDocument containing the badge template
        var badgeXml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber);

        // Get the "badge" element
        var badgeElement = (XmlElement)badgeXml.GetElementsByTagName("badge").First();

        // Set the count in its "value" attribute (as string)
        badgeElement.SetAttribute("value", count.ToString());

        // Create a badge notification from the XML content.
        var badgeNotification = new BadgeNotification(badgeXml);

        // Send the badge notification to the app's tile.
        BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeNotification);
    }

Upvotes: 2

Related Questions