2016-08-14 67 views
1

我有一個簡單的UWP購物清單應用程序,它什麼也談不到,並保留其數據在本地存儲。當應用程序暫停時,我將當前列表寫入本地存儲。我想在同一時間執行的操作(或主要頁面OnNavigatedFrom)也是更新實時磁貼以顯示當前列表中的項目數量,以便用戶無需啓動應用程序即可看到此事實。簡單的UWP活動磁貼更新

我無法找到與此類簡單案例相適應的文檔/示例...沒有後臺任務,沒有推送通知,在某個時間表上沒有任何事情發生,只是基本上響應用戶操作。我可以找到的例子似乎涉及使用TileUpdater,但我不明白需要一個,或者如果/如何應該在這種簡單的情況下使用它。

在我的代碼中,我必須做更新的最小步驟順序是什麼?一個徽章計數(如果計數爲零,那麼這兩個步驟都會將值刪除並刪除)?

回答

2

找到我自己的答案。

我創建到我添加了以下方法,我稱之爲每當我需要更新徽章計數服務類:

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

這種方式爲我工作。要移除徽章,只需傳入0。 – Del