:::: MENU ::::

How to pin a HubTile to start screen

One of the questions on my blog (I love when people ask smart questions and leave nice comments :) ) was how to pin a hubtile to start screen, as a normal live tile. This post will describe the method, continuing on the article about the hubtiles I wrote earlier. I created a few hubtiles in that article and now I want to be able to pin the hubtile by using the context menu on hold event, to resemble the usual procedure for pinning. For the purpose of this article, we will use the ContextMenu that opens on hold with only one MenuItem – “pin to start” which then pins the hubtile as a secondary live tile on start screen.

If you don’t really feel like reading my old article about HubTiles, let me get you up to speed. We used 5 hubtiles defined in XAML using the images that are part of the official hubtile example that comes with the Silverlight toolkit. Every hubtile has a ContextMenu attached to it. The XAML code is:

This code is pretty much copy pasted from my old article, except for one important thing – every hubtile now has a ContextMenu, such as this one:

with only one MenuItem that reacts to Tap event with the MenuItem_Tap event handler. Each hubtile has it’s own MenuItem tap event handler, which calls the main function for creating the secondary live tile, called CreateLiveTile.

The code behind for the MainPage.xaml is very simple, and consists of all the MenuItem tap event handlers:

It’s clear what all the event handlers are doing – calling the CreateLiveTile method with only one parameter – the reference to the hubtile which should get pinned.

The CreateLiveTile is where all the magic happens. :) Let’s first look at the sample code for creating a secondary live tile at the MSDN documentation. Basically, I first get all the tiles from the Phone and check if there’s already a tile which has the NavigationUri property the same as the one I’m trying to create. Why does the secondary tile need the NavigationUri property at all? It’s because I’m creating those tiles to take me to different parts and functionalities of the app, and it’s what makes secondary tiles different. Since this is just a short article and demo, and not a real Marketplace app, every pinned tile will have NavigationUri taking me to MainPage.xaml, but will have a different passing parameter – it will be the hubtile Title, which ensures that I can pin all the five hubtiles.

So, if a tile with such a NavigationUri doesn’t exist, I create it. In other words, I first create a new StandardTileData object from the hubtile I want to pin:

Then I check to see if the tile already exists. If it doesn’t, I create it, and if it does, I show a MessageBox saying the the tile has already been pinned:

So this is it – now you know how to pin a hubtile to startscreen using a context menu. The only thing that’s different is that every live tile that you pinned has the background color the same as the accent color of your phone. This is because we didn’t set the Background property of the StandardTileData object. In a hubtile you set the background as a brush by simply naming it, and in a StandardTileData you set the background as a Uri to a image in your project. There are many workarounds to make this transition simple, but it’s beyond the scope of the article. I may describe it in one of my next articles, if someone finds it necessary. :)

If you find the article useful, make sure others hear about it, too. :)

Share the knowledge

No Comments

  • Reply Pyash |

    Thanks for the great article. I couldn’t get the image to be displayed in the Tile using your sample code. I had to write the image to the isostore:/Shared/ShellContent/tile and read it from there. I hope there is a better way to do this.

    • Reply igrali |

      Hi

      Thanks for reading!

      I can hardly tell what went wrong and why can’t you read the image like this. If the image has been properly set on the hubtile inside the app, it should work… It worked for me.

      I can find the old code and give it a try again to see if I can detect the possible issue.

  • Reply Pyash |

    Thanks for the great article. I couldn’t get the image to be displayed in the Tile using your sample code. I had to write the image to the isostore:/Shared/ShellContent/tile and read it from there. I hope there is a better way to do this.

    • Reply igrali |

      Hi

      Thanks for reading!

      I can hardly tell what went wrong and why can’t you read the image like this. If the image has been properly set on the hubtile inside the app, it should work… It worked for me.

      I can find the old code and give it a try again to see if I can detect the possible issue.

  • Reply Pyash |

    Sorry for the late reply. I found the issue why I couldn’t get the images displayed using your code. I had the Build Action on the images set to Resource. Changed the build action to content and worked like a charm. Thanks again.

  • Reply Pyash |

    Sorry for the late reply. I found the issue why I couldn’t get the images displayed using your code. I had the Build Action on the images set to Resource. Changed the build action to content and worked like a charm. Thanks again.

    • Reply igrali |

      Glad to hear you got it working with setting the build action to Content.

      Now, I’m not sure about the font size. Perhaps there’s some sort of a property. If not, you will have to edit the styling yourself… I’ll look into it!

    • Reply igrali |

      Glad to hear you got it working with setting the build action to Content.

      Now, I’m not sure about the font size. Perhaps there’s some sort of a property. If not, you will have to edit the styling yourself… I’ll look into it!

So, what do you think ?