Uncategorized

Live Tiles in Windows Phone 8: Part 1 Tile Templates

Live tiles are one of the signature features of Windows Phone and are used to represent your app on the start screen of the phone and give the user quick access to your app. Live tiles also enable apps to show notifications to the user on the home screen and starting with Windows Phone 8 – on the lock screen. While, the Primary (or default) tile appears on the phone’s start screen only when a user pins your application, secondary tiles are created programmatically by the application and multiple such tiles can be active at the same time (to display, for example, the weather in several cities).

Windows Phone 8 brings several new Live Tile features: three new Live Tile templates; Live Tiles can now support three sizes; information from the Primary tile can now be displayed on the lock screen.

New Live Tile templates

Three new Tile templates are available in Windows Phone 8:

  • Flip – this is the regular live tile that we are used to seeing in Windows Phone 7
  • Iconic – similar to the Flip tile but follows more closely the Windows Phone design principles and displays an icon instead of an image
  • Cycle – can cycle up to 9 images, similar to the Pictures live tile

New Live Tile sizes and resolutions

Windows Phone 8 supports the following Tile sizes: small, medium, and wide / large.

NOTE: Tile size is selected by the user from the phone’s home screen, but you can control which sizes a tile supports. It is recommended that you provide a separate image for each size.

image_2_11

wp8-tile-small_2 wp8-tile-medium_2 wp8-tile-large_2

 

Flip and Cycle

Iconic

Small

159 × 159 px

110 × 110 px

Medium

336 × 336 px

202 × 202 px

Wide

691 × 336 px

N/A

wp8-tiles-sample_2 NOTE: Windows Phone 8 supports two additional resolutions, WVGA and 720p. All images are scaled by the runtime to work for these resolutions appropriately.

In this article we will create a sample application that displays information about different fast foods using the different types of tiles. In order to demonstrate how the app can display notifications on the phone’s lock screen, we will also implement a background agent that periodically updates the information displayed by the app’s Primary tile. The app demonstrates all three tile templates:

  • Flip tile – we will use the flip tile template for the app’s primary tile
  • Iconic tile – the sample app also demonstrates how easy it is to use the iconic tile template
  • Cycle tile – we will demonstrate how to display a cycle tile that cycles through several images

Let’s start building the app by making our Main Page look like the screen-shot above. Here is the XAML code that you have to put in MainPage.xaml:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Value, ElementName=countSlider, StringFormat='Count: {0}'}" />
<Slider x:Name="countSlider" />
<TextBlock Text="Content:" />
<TextBox x:Name="txtContent" />
<Button x:Name="btnUpdatePrimaryTile" Content="Update primary tile" Click="btnUpdatePrimaryTile_Click" />
<CheckBox x:Name="cbEnableTileUpdateAgent"
Content="Enable tile update agent"
Checked="cbEnableTileUpdateAgent_Checked"
Unchecked="cbEnableTileUpdateAgent_Unchecked" />

<CheckBox x:Name="cbEnableIconicTile"
Content="Enable iconic tile"
Checked="cbEnableIconicTile_Checked"
Unchecked="cbEnableIconicTile_Unchecked" />

<CheckBox x:Name="cbEnableCycleTile"
Content="Enable cycle tile"
Checked="cbEnableCycleTile_Checked"
Unchecked="cbEnableCycleTile_Unchecked" />
</StackPanel>
</Grid>

Using the Flip Tile template

Since in WMAppManifest.xml, we have selected to use the Flip tile template for our Primary tile, in order to implement the “Update primary tile” functionality we will have to use the FlipTileData class which represents the Flip tile template. Let’s start by implementing the click handler for the button:

private void btnUpdatePrimaryTile_Click(object sender, RoutedEventArgs e)
{
int count = (int)this.countSlider.Value;
string content = this.txtContent.Text;

TileUpdateAgent.TileUpdateScheduledAgent.UpdatePrimaryTile(count, content);
}

In that click event handler, the actual updating is done by the static UpdatePrimaryTile method of our TileUpdateScheduledAgent class (we’ll return to that background agent later). Here is how this method looks like:

public static void UpdatePrimaryTile(int count, string content)
{
FlipTileData primaryTileData = new FlipTileData();
primaryTileData.Count = count;
primaryTileData.BackContent = content;

ShellTile primaryTile = ShellTile.ActiveTiles.First();
primaryTile.Update(primaryTileData);
}

What this method does in order to update the primary tile is create a new FlipTileData instance and set the Count and BackContent properties. Then it retrieves the primary tile from the ActiveTiles collection of the ShellTile class. (NOTE: the primary tile is always the first tile, even if it isn’t pinned on the home screen). Finally, we trigger the update using the Update method of the primary Tile object that we just retrieved.

Using the Iconic Tile template

Using the new Iconic tile template, is probably the easiest way to implement a live tile in Windows Phone 8.In our sample app, a secondary tile that uses the Iconic tile template is created when the “Enable iconic tile” check box is checked and is removed when the check box is unchecked. Let’s start by implementing the Checked and Unchecked event handlers for the check box:

private static readonly string IconicTileQuery = "tile=iconic";

private void cbEnableIconicTile_Checked(object sender, RoutedEventArgs e)
{
if (this.ignoreCheckboxEvents)
{
return;
}
Uri tileUri = new Uri(string.Concat("/MainPage.xaml?", IconicTileQuery), UriKind.Relative);
ShellTileData tileData = this.CreateIconicTileData();
ShellTile.Create(tileUri, tileData, true);
}

private void cbEnableIconicTile_Unchecked(object sender, RoutedEventArgs e)
{
if (this.ignoreCheckboxEvents)
{
return;
}
ShellTile iconicTile = this.FindTile(IconicTileQuery);
if (iconicTile != null)
{
iconicTile.Delete();
}
}

There are several things that deserve attention in the code snippet above. First, notice how we are adding a query string to the tile’s URI that will allow us to find the tile later, when we want to remove (or update) it. Then in order to create the tile, we use the Create method of the ShellTile class. Finally, to remove the tile, we call the Delete method of the tile once we have found it using the FindTile method (which we will show in a moment).

Here is the CreateIconicTileData method that creates the tile definition using the Iconic tile template:

private ShellTileData CreateIconicTileData()
{
IconicTileData iconicTileData = new IconicTileData();
iconicTileData.Count = 11;
iconicTileData.IconImage = new Uri("/Assets/pizza.lockicon.png", UriKind.Relative);
iconicTileData.SmallIconImage = new Uri("/Assets/pizza.lockicon.png", UriKind.Relative);
iconicTileData.WideContent1 = "Wide content 1";
iconicTileData.WideContent2 = "Wide content 2";
iconicTileData.WideContent3 = "Wide content 3";

return iconicTileData;
}

And this is FindTile method that we are using to find tiles in the ActiveTiles collection by (part of ) their navigation URI:

private ShellTile FindTile(string partOfUri)
{
ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault(
tile => tile.NavigationUri.ToString().Contains(partOfUri));

return shellTile;
}

Now if you launch the app and enable the iconic tile, you should see something like this:

image_4_11

Using the Cycle Tile template

The Cycle tile template enables you to create a tile that cycles through up to 9 different images, similarly to the tile of the built-in Pictures app.Adding and deleting tiles that use the Cycle tile template is done in the same way as with the other tile types. The biggest difference is how you create the tile definition. To create the Cycle tile we are using the CreateCycleTileData method:

private ShellTileData CreateCycleTileData()
{
string[] imageNames = {
"bonfillet.jpg", "bucket.jpg", "burger.jpg", "caesar.jpg",
"chicken.jpg", "corn.jpg", "fries.jpg", "wings.jpg"
};

CycleTileData cycleTileData = new CycleTileData();
cycleTileData.Title = "Cycle tile";
cycleTileData.Count = 7;
cycleTileData.SmallBackgroundImage = new Uri("/Assets/pizza.lockicon.png", UriKind.Relative);
cycleTileData.CycleImages = imageNames.Select(
imageName => new Uri(string.Concat("/Assets/Images/", imageName), UriKind.Relative));

return cycleTileData;
}

For the sake of completeness, here is the code for the Checked and Unchecked event handlers of the “Enable cycle tile” check box. The only difference is that we are using a different query string in the navigation URI for the cycle tile:

private static readonly string CycleTileQuery = "tile=cycle";

private void cbEnableCycleTile_Checked(object sender, RoutedEventArgs e)
{
if (this.ignoreCheckboxEvents)
{
return;
}
Uri tileUri = new Uri(string.Concat("/MainPage.xaml?", CycleTileQuery), UriKind.Relative);
ShellTileData tileData = this.CreateCycleTileData();
ShellTile.Create(tileUri, tileData, false);
}

private void cbEnableCycleTile_Unchecked(object sender, RoutedEventArgs e)
{
if (this.ignoreCheckboxEvents)
{
return;
}
ShellTile cycleTile = this.FindTile(CycleTileQuery);
if (cycleTile != null)
{
cycleTile.Delete();
}
}

If you now run the app and enable the cycle tile, you should see on the phone’s start screen, a tile that displays fast food images, as in the following screen-shots.

image_6_11

Conclusion

This article demonstrated the new Tile templates that come with the Windows Phone 8 SDK.  Stay tuned for the next post: Live Tiles in Windows Phone 8: Part 2 Lock Screen Notifications!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.