Before I go too nuts with explaining this new code, I thought I’d mention that all of my new library helpers for Windows Universal apps will be published on my GitHub account. This just makes it easier for people to find some of the code that I’m sharing in an easier way, plus it becomes easier for you to just grab a copy. And if I make a mistake, the whole internet can help make this code better!

On to updating live tiles. In Windows Phone Silverlight live tiles were updated by populating strongly typed objects with the data one wanted. When ready, you send it off to the updater and voila, your tile is now live! Now in WinRT, you have access to get an XML template that you can merge your data into. This has some great advantages because there are TONS of templates, check them all out. It is also a really flexible approach. But I still am a little bit more comfortable with a more structured way of passing in my data.

LiveTileThe second drawback for me personally, is that live tiles consist of multiple sizes that you want to update at the same time. In order to do this in the WinRT style, you have to do some potentially awkward Xml node merging. In the MSDN example, it shows only two tiles, imagine your code with another tile in there. There has to be a better way.

Let’s look at the final usage below. There is a LiveTileSet object that contains three properties that take in the data we want. When we are ready, there is an extension method .PushToLiveTile() that does the final merging. This was made where we can pass extra data that a template doesn’t support and it won’t die. However, if you try passing a TileTemplateType to a tile that cannot support it, you will get a runtime exception. I like this approach, I am forced to structure my data, but I don’t have to worry about how the data gets to the tile. I also retain all the flexibility of the templates, with access to all of the existing templates.

On to the implementation under the hood. I’ll skip over the classes that are there to transfer data, but understand that the LiveTileSet class will validate the TileTemplateType that was passed to it.

PushToLiveTile does two things, it creates a list of all non-null properties in the tileset that you pass in, and then passes that into the UpdateLiveTile function… no magic here.

UpdateLiveTile loops through the list of tiles that we now have and passes the individual tile data to our last method. As it gets the XML back, it performs the merging of the tile. Thank you abstraction! Once we have everything, it actually passes the data to the TileUpdateManager to perform the update.

The last core function that is called for each tile we need is GetTileXml. This grabs the XML template from the TileUpdateManager and assigns the tile data to the template attributes. The big logic here is determining how many text and image fields there are in the template and in our data. Whichever one contains less is the amount of times we loop through and execute the assignments. This allows us to pass too much data, there could be data loss. I feel this is ok since we are still going to get a Tile update.

Now that we have this helper, we don’t have to think about the mechanics of updating the live tile. There is no magic, just a bunch of XML manipulation. Check out the GitHub repo for this code and I will keep adding more to it as I keep making more Windows and WindowsPhone WinRT Universal Applications!

 

 

Leave a Reply
Your email address will not be published.
  • ( will not be published )