下面將介紹兩種方式如何在windows10 uwp開發中如何更新應用磁貼:html
實際上windows的磁貼就是用xml實現的,你只須要建立相應格式的xml就能夠實現動態磁貼了windows
一,手動更新磁貼服務器
二,輪詢更新磁貼網絡
【第一種方式】手動更新磁貼app
private void changeBtn_Click(object sender, RoutedEventArgs e) { //獲取模板 var tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Text01); //獲取標籤名爲text的元素值 var tileAttributes = tileXml.GetElementsByTagName("text"); //將獲取的元素值追加一個值 tileAttributes[0].AppendChild(tileXml.CreateTextNode(titleBox.Text)); //建立一個磁貼類,將xml數據填充到磁貼中 var tileNotification = new TileNotification(tileXml); //向磁貼更新 TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification); }
這樣,當點擊按鈕時,應用磁貼就會顯示文本框輸入的值佈局
【第二種方式】輪詢更新磁貼spa
若是你不瞭解MVC你能夠跳過此步驟,而後看下面如何經過網絡獲取數據code
並在控制器的Index默認方法裏添加如下數據:xml
public ActionResult Index() { ViewBag.Title = "巴黎發生恐襲"; ViewBag.Content = "數百人遇難"; ViewBag.Type = "新聞"; return View(); }
而後再Hello視圖中添加Index視圖,把佈局頁模板頁什麼的勾選去掉,而後新建了一個.cshtml文件,最後你要把Index.cshtml中的全部html數據所有清空替換成如下文檔結構htm
<tile> <visual version="2"> <binding template="TileSquare150x150Text01" fallback="TileSquareText01"> <text id="1">@ViewBag.Title</text> <text id="2">@ViewBag.Content</text> <text id="3">@ViewBag.Type</text> <text id="4">@DateTime.Now.ToLongTimeString()</text> </binding> </visual> </tile>
待會再介紹模板問題
最後一步,回到UWP項目中,建立一個按鈕(當按下這個按鈕時,之後磁貼會按照你設定時間進行自動輪詢更新),添加一個單擊事件事件,添加如下代碼:
private void AutoUpdate_Click(object sender, RoutedEventArgs e) { //設置一個Uri類型變量保存服務器的xml地址 var tileContent = new Uri("http://localhost:61341/UWP/Index"); //設置輪詢時間變量爲半小時,也能夠設置其餘時間 var requestedInterval = PeriodicUpdateRecurrence.HalfHour; //建立磁貼更新實例 var updater = TileUpdateManager.CreateTileUpdaterForApplication(); //開始輪詢更新,傳入服務器磁貼xml文件地址和輪詢時間 updater.StartPeriodicUpdate(tileContent, requestedInterval); }
這樣一來你的應用就實現了自動磁貼更新的功能。
下面咱們再來討論一下磁貼模板問題: