One of the attractive features of Windows Phone 7 is the tiles in the start menu. It’s pretty easy to point the main application tile (and secondary tiles) to static URLs. It’s a bit more difficult to create a dynamic image for a live tile.
Starting with tiles
The easiest way to start is by tweaking the WMManifest.xml file (or even easier, but less flexible) the Project Properties Application Tab:

The title appears as text under the background image. Ideally, the image should be 173x173 pixels in size. Otherwise, WP7 will stretch/crop as appropriate. The image must be a PNG or JPG file. Using a transparent PNG will make the phone’s accent color show through. Here’s an example with an alternate image (viewed with the emulator):

The manifest file contains more options than the above Properties section. This is how it looks now:
<TemplateType5>
<BackgroundImageURI IsRelative="true" IsResource="false">note.png</BackgroundImageURI>
<Count>0</Count>
<Title>Quick Notes</Title>
</TemplateType5>
This is located in the lower part of the file. The Count element allows placing some number between 1 and 99 in the top right corner of the icon. Here’s how it looks when set to the number 5:

This may come in handy.
The new Mango version adds the ability to have a background tile as well, flipping every couple of seconds or so:
<TemplateType5>
<BackgroundImageURI IsRelative="true" IsResource="false">note.png</BackgroundImageURI>
<Count>5</Count>
<Title>Quick Notes</Title>
<!--<BackBackgroundImageUri IsRelative="true" IsResource="false">background.jpg</BackBackgroundImageUri>-->
<BackTitle>Back Title</BackTitle>
<BackContent>Some content</BackContent>
</TemplateType5>
Here’s how the back tile looks:

Although there is a BackBacgroundImageUri, it doesn’t seem to work from the XML (but can be set in code).
Changing a Tile Programmatically
This is fine if all we want is something that does not change. To update a tile, however, or to create secondary tiles, we need some code.
Let’s create a quick notes application that allows adding notes that may be pinned to the start menu. The main tile would indicate the number of existing notes.
Updating the main tile can be done like so:
var mainTile = ShellTile.ActiveTiles.First();
mainTile.Update(new StandardTileData {
Count = _notes.Count
});
The ShellTile class is the main gateway to tiles. The ActiveTiles static collection is a list of all application tiles. The list includes at least the main tile (always the first tile), even if it’s not actually pinned.
To update the tile, we create a StandardTileData object and fill in the required properties. In this case, only the Count is changed, but any other property (such as Title and BackgroundImage can be changed).
Creating new tiles
How about creating new tiles? Essentially it’s not too difficult: fill in a StandardTileData object and call Shelltile.Create:
var data = new StandardTileData {
Title = note.Name,
BackTitle = note.Name,
BackContent = note.BackText,
};
ShellTile.Create(new Uri("/NoteData.xaml?name=" + data.Title, UriKind.Relative), data);
The first argument to Create is a URI that would be followed if the user clicks on the tile. In this case, I’m forwarding this to a XAML page in the app, that would use the query string to display the full note information.
What about the image of the tile? if it’s a predefined image (either within the app or on some server reachable by HTTP), then it’s easy – just set the BackgroundImage property to the appropriate URI. But what about something more dynamic?
Suppose we want the main note text to be displayed in the main tile area (as an image). How could we construct that?
There are 2 steps involved:
1. Create a dynamic image
2. Provide some URI that can reach it
The BackgroundImage property is a URI. I would have expected an ImageSource-derived object, but alas, that is not the case. We have to use a URI somehow.
We’ll use the WriteableBitmap class to create a dynamic image consisting of the note text. One of WriteableBitmap’s abilities is to render any UIElement inside it, so we’re practically not limited to anything.
First, we’ll create a user control that would host our content. Then we’ll render its contents into a WriteableBitmap. Here’s a simple user control markup suitable for our purposes:
UserControl x:Class="QuickNotes.DynamicNote"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="173" d:DesignWidth="173" x:Name="ctl" Width="173" Height="140"
>
<Border Background="{StaticResource PhoneAccentBrush}">
<TextBlock x:Name="_text" Margin="6" Width="173" Height="140" Padding="4" HorizontalAlignment="Center"
TextWrapping="Wrap" VerticalAlignment="Center" />
</Border>
</UserControl>
This is essentially a simple TextBlock enclosed in a border, that is colored with the user’s theme (PhoneAccentBrush resource key).
Now let’s draw that into a bitmap:
var ctl = new DynamicNote(note.Text);
ctl.Measure(new Size(173, 173));
ctl.Arrange(new Rect(0, 0, 173, 173));
var bmp = new WriteableBitmap(173, 173);
bmp.Render(ctl, null);
bmp.Invalidate();
Note that we need to call Measure and Arrange if the element to render is not part of a visual tree. Failing to do so will produce unpredictable results (try it!).
The next step is to somehow transform that into a URI. To do that, we’ll save this bitmap to isolated storage (the storage every application has) in a special folder named Shared/ShellContent and create a URI from that:
var iss = IsolatedStorageFile.GetUserStoreForApplication();
var filename = "/Shared/ShellContent/note" + note.Name + ".jpg";
using(var stm = iss.CreateFile(filename)) {
bmp.SaveJpeg(stm, 173, 173, 0, 80);
}
// pin
var data = new StandardTileData {
Title = note.Name,
Count = null,
BackTitle = note.Name,
BackContent = note.BackText,
BackBackgroundImage = new Uri("note.png", UriKind.Relative),
BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute)
};
ShellTile.Create(new Uri("/NoteData.xaml?name=" + data.Title, UriKind.Relative), data);
The SaveJpeg is an extension method for WriteableBitmap. It certainly comes in handy in this case. Note the new image URI starting with the “isostore:” prefix.
That’s it. We have a dynamic live tile (lower left):

Deleting (unpinning) a tile
To delete (unpin) a tile programmatically, just call the ShellTile.Delete instance method. Here’s an example:
var tile = ShellTile.ActiveTiles.Single(t => t.NavigationUri.ToString().Contains("?" + note.Name + "="));
tile.Delete();
We first must find the tile we want to remove using some properties we’re aware of (typically the NavigationUri property). Then the single call to Delete is all it takes to get rid of the tile.