Original: UWP WINDOWS10 developing update tiles and dynamic update tiles
Here are two ways to update your app's tile in WINDOWS10 UWP development:
In fact, the Windows tile is implemented in XML, you just need to create the appropriate format of XML to implement the live tile
One, manually update the tile
Second, poll the update tile
"First Way" manual update of Tiles
- Create a text box and a button that will display the text of the text box when the button is clicked
- To bind a button to an event, the code is as follows
Private voidChangeBtn_Click (Objectsender, RoutedEventArgs e) { //Get Template varTilexml =tileupdatemanager.gettemplatecontent (tiletemplatetype.tilesquare150x150text01); //gets the element value with the label name text varTileattributes = Tilexml.getelementsbytagname ("text"); //appends the obtained element value to a valuetileattributes[0]. AppendChild (Tilexml.createtextnode (Titlebox.text)); //Create a tile class that fills the XML data into a tile varTilenotification =Newtilenotification (Tilexml); //Update to Tiletileupdatemanager.createtileupdaterforapplication (). Update (tilenotification); }
This way, when the button is clicked, the app tile displays the value entered by the text box
"Second Way" poll update tile
- First you have to have a server, or you can parse third-party data into the tile XML format before polling
- The following describes using MVC as the server side, and then the UWP gets the data once every half hour to the server
If you don't understand MVC you can skip this step and see how to get the data over the network below
- Create an MVC Project
- Add a Hello Controller
and add the following data to the controller's index default method:
Public actionresult Index () { " terrorist attacks in Paris "; " hundreds of killed. " ; " News " ; return View (); }
Then add the index view in the Hello view, remove the Layout page template page, and then create a new. cshtml file, and finally you will replace all the HTML data in the index.cshtml with the following document structure
<Tile> <Visualversion= "2"> <bindingTemplate= "tilesquare150x150text01"fallback= "TileSquareText01"> <textID= "1">@ViewBag. Title</text> <textID= "2">@ViewBag. Content</text> <textID= "3">@ViewBag. Type</text> <textID= "4">@DateTime. Now.tolongtimestring ()</text> </binding> </Visual></Tile>
I'll introduce you to the template later.
In the final step, go back to the UWP project and create a button (when you press this button, the tile will automatically poll for updates as you set the time), add a Click event event, and add the following code:
Private voidAutoupdate_click (Objectsender, RoutedEventArgs e) { //set a URI type variable to hold the server's XML address varTilecontent =NewUri ("Http://localhost:61341/UWP/Index"); //Set the polling time variable to half an hour, or you can set another time varRequestedinterval =Periodicupdaterecurrence.halfhour; //To create an instance of a tile update varUpdater =tileupdatemanager.createtileupdaterforapplication (); //start polling for updates, incoming server tile XML file address and polling timeUpdater. Startperiodicupdate (Tilecontent, requestedinterval); }
This allows your app to have automatic tile updates.
Let's discuss the tile template problem:
- Different tile sizes have different formats and you want to set different templates for each tile at the same time
- Templates have fixed format and different resource types, you can refer to:https://msdn.microsoft.com/zh-cn/library/windows/apps/br212859.aspx
- Thank you!
UWP WINDOWS10 Developing update tiles and dynamic update tiles