I’ve used a new control in my latest demo app, called HubTile which is a part of the latest Silverlight for Windows Phone toolkit (August – Mango release) In this post I will describe how to use HubTiles, including how to style them to be more appropriate for your own application.
HubTiles let you easily use tiles inside of your application. I’ve seen a numerous forum posts where people asked about creating a button that looked like a tile – large square with no borders, that responded to tapping. Well, the answer to those was almost always styling a button, but now you can use the same tiles that are all over the home screen of your Windows Phone. How to use them? Let’s create a small application. First of all, create a new Windows Phone Pivot application, targeting version 7.1. The easiest way to do it is creating a new Page project and adding a Pivot control. After that, you should have the following code as your layout in the Pivot control:
|
1 2 3 4 5 6 7 8 |
<Grid x:Name="LayoutRoot" Background="Transparent"> <controls:Pivot Title="HUB TILES"> <controls:PivotItem Header="first"> </controls:PivotItem> <controls:PivotItem Header="second"> </controls:PivotItem> </controls:Pivot> </Grid> |
Now, make sure you add the latest Silverlight for Windows Phone toolkit to your project. The easiest way to do it is to go to
Tools -> Library Package Manager -> Package Manager Console
In the console, write:
Install-Package SilverlightToolkitWP
And press Enter. There it is, added to your References! Current version is 4.2011.8.17. Now, add it to your MainPage.xaml (one line, not two!):
|
1 2 |
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls; assembly=Microsoft.Phone.Controls.Toolkit" |
Add a new folder Images to your project, and add 5 images bulding them as Content. They should be 173×173, to fill the HubTile. I added dummy images, the images from the official examples. In the first pivot item, add a grid 2*3.
|
1 2 3 4 5 6 7 8 9 10 11 |
<Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> </Grid> |
You can now go ahead and add HubTiles to the Grid:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<toolkit:HubTile Grid.Row="0" Grid.Column="0" GroupTag="hubs" Source="Images/Shrimp.jpg" Title="Shrimp" Background="Red" Message="I love shrimps" Margin="0,0,10,10" Name="ShrimpTile"/> <toolkit:HubTile Grid.Row="0" Grid.Column="1" GroupTag="hubs" Source="Images/Dessert.jpg" Title="Dessert" Background="Blue" Message="I love desserts" Margin="0,0,10,10"/> <toolkit:HubTile Grid.Row="1" Grid.Column="0" GroupTag="hubs" Source="Images/Seattle.jpg" Title="Seattle" Message="I'll go to Seattle" Background="Gold" Margin="0,0,10,10"/> <toolkit:HubTile Grid.Row="1" Grid.Column="1" GroupTag="hubs" Source="Images/Venice.jpg" Title="Venice" Background="Green" Message="I'll go to Venice in the meantime" Margin="0,0,10,10"/> <toolkit:HubTile Grid.Row="2" Grid.Column="0" GroupTag="hubs" Source="Images/Pretzel.jpg" Title="Pretzels" Background="Magenta" Message="Pretzels are good for breakfast" Margin="0,0,10,10"/> </Grid> |
As you can see, a HubTile has a several important properties.
- Source – source of the image you will use for the Tile
- GroupTag – a tag for the tiles that belong to the same group – useful for freezing more tiles at a time
- Title - shown on tile in large font
- Background – background color of the tile – background for the Message text
- Message – a short note on the back of the tile, shown in small font
Results in the emulator:
Now, can we tap on the tile?
Using gestures with HubTiles
Of course we can! In order to be able to react on tapping the tile, we need to add an event handler for the Tap event
|
1 2 3 4 5 6 7 8 9 |
<toolkit:HubTile Grid.Row="1" Grid.Column="0" GroupTag="hubs" Source="Images/Seattle.jpg" Title="Seattle" Message="I'll go to Seattle" Background="Gold" Margin="0,0,10,10" Tap="SeattleTap"> </toolkit:HubTile> |
This adds the event handler in the code behind, in which you can navigate to another page, for example:
|
1 2 3 4 5 6 |
private void SeattleTap(object sender, System.Windows.Input.GestureEventArgs e) { NavigationService.Navigate(new Uri("/SeattlePage.xaml", UriKind.Relative)); } |
There needs to be a SeattlePage.xaml page added before, of course. And you can use any other gesture the same way!
Styling a HubTile
In case you wanted to make a HubTile bigger than 173×173, your first try would probably be:
|
1 2 3 4 5 6 7 8 9 10 11 |
<toolkit:HubTile Grid.Row="1" Grid.Column="0" GroupTag="hubs" Source="Images/Seattle.jpg" Title="Seattle" Message="I'll go to Seattle" Background="Gold" Height="300" Width="300" Margin="0,0,10,10" Tap="SeattleTap"> |
And that doesn’t work. You need to change the style of the HubTile. You can do it in Blend using a very nice graphical interface, or you can directly write XAML code.
I did it in Blend by clicking on one of the HubTiles then Edit Template -> Edit a copy. It creates a huge amount of code which you have to edit. Basically, you need to edit the size of the control elements, but not just that – you need to edit the code for animations too. All the code is created for you in Blend. All you need to do is change the values. So wherever it says 173, put the size YOU want (in this case, 300), and wherever it says -173, put the negative value of the size you want (in this case, -300). As simple as that. You can change the size of the text on the HubTile, too, but that depends on how big your tile actually is. :)
You can download the style for the size 300×300 here. Just add it to your page resources.
Freezing HubTiles
To improve the performance of your app, make sure that you don’t animate the HubTiles when you don’t see them – there’s no use in that. That’s what the Freeze option is for. That way, when you go to another Pivot or Panorama item, simply freeze one or more HubTiles.
To freeze one HubTile, use:
|
1 |
HubTileService.FreezeHubTile(ShrimpTile); |
And to freeze a group of several HubTiles, use:
|
1 |
HubTileService.FreezeGroup("hubs"); |
This is what you need to set the GroupTag value for. To unfreeze one HubTile, use:
|
1 |
HubTileService.UnfreezeHubTile(ShrimpTile); |
and to unfreeze a group of several HubTiles when you can see them again, use:
|
1 |
HubTileService.UnfreezeGroup("hubs"); |
Notice that when you use a single HubTile, you pass the object as the argument to the method, and when you use a group of HubTiles, you pass the name of the group. If you want to see how it works, use the Pivot control you’ve created in the beginning, with 2 Pivot items. On pivot selection changed, do:
|
1 2 3 |
if (mypivot.SelectedIndex == 1) HubTileService.FreezeGroup("hubs"); else HubTileService.UnfreezeGroup("hubs"); |
You’ll notice that if you let the HubTiles animate for a while, then go to the second pivot item, wait a little and then come back to the first pivot item, the tiles are just like you left them!
I hope that this post will help you in your own use of HubTiles – one of the prettiest and most useful controls in the latest release of the Silverlight toolkit for Windows Phone. Sorry for the code styling – I am still finding a way to hosting the blog myself - then I will use addons for better code styling. :)
P.S. Perica, my first application, was updated a few days ago, with a few new features. You can find more details here: http://www.windowsphoneapplist.com/perica-a26819.html



Thanks of all. I m waiting tha you write other control of Silverlight for Windows Phone toolkit (August – Mango release) .
bye
Great post, this helped me this morning with a tile sizing problem. Thanks!
I am very glad it helped! :)
Regards
Is there a way to span a tile to 2 columns? I found all tiles have the same size make the UI very boring.
I don’t think you can simply span them – try resizing the like I explained in the article, that could help!
Regards
How do I pin these hubtiles to start screen? Do I have to use the Hold event?
I will cover this in my next post very soon! :)
and here it is: http://igrali.wordpress.com/2011/09/27/how-to-pin-a-hubtile-to-start-screen/ :)
How can i add an event to the hub tile like onclick or onfocus (it should navigate to another page) ?
Like in the example in the article:
<toolkit:HubTile Grid.Row="1"
Grid.Column="0"
GroupTag="hubs"
Source="Images/Seattle.jpg"
Title="Seattle"
Message="I'll go to Seattle"
Background="Gold"
Margin="0,0,10,10" Tap="SeattleTap">
where SeattleTap is the event handler for the Tap event.
In that event handler you can navigate to another page.
Hi, first of all, thank you very much! it’s a great topic!
Then, i’ve got a problem, i tried to put a picture with transpancy as source for the tile, but, when my source in on front, i can see the reverse of the tile with the message (reversing to o_0).
Does anyone have experiment this problem and find a solution?
lol same here..tell me if u find a away,guess must play around with the template
I haven’t had the same problem, so I can’t really help you, sorry…
My search for tiles inside WP application ended here!
Have you ever thought about writing an ebook or guest
authoring on other websites? I have a blog centered on the same topics you discuss and would love
to have you share some stories/information. I know my
readers would enjoy your work. If you’re even remotely interested, feel free to shoot me an email.
You have the ultimate writing form. Like Jd Sallinger or
someone.
Does your site have a contact page? I’m having a tough time locating it but, I’d like to send you an email.
I’ve got some suggestions for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it improve over time.
“How to use the HubTile control | Igor on Mobile” was really enjoyable and
educational! Within the present day society honestly, that is really hard to manage.
Thx, Alicia
Hello There. I discovered your blog the usage of msn. This is an extremely neatly
written article. I will make sure to bookmark it and come back to
learn extra of your helpful information. Thank you for the
post. I will certainly comeback.