Microsoft Style Dynamic Tiles for Windows Phone Mango

Mail Tile Image

Update: I’ve published the code for this post on github. Get the code here.

Have you ever wanted to create a transparent tile for your app that mimics the way the built in Messaging and Mail apps work on your Windows Phone? I’m going to provide specifics on how to mimic the style and do it all on the phone. Prior to Mango, there were only a couple ways to do this and both were nasty. With Mango, we can easily manipulate the primary and secondary tiles for our apps in code.

My goal is to provide a visual way to setup your tile and eliminate the need to write any unnecessary code. There’s no need to write any code to manipulate what’s actually on the tile (i.e. text, images, etc.) since we’ve already been provided a sophisticated layout tool by Microsoft, the visual designer of Visual Studio or Expression Blend. I’ve provided a sample project with all the source code for you to use freely in your projects. The above image is what we’re trying to achieve.

Let’s break down some of the details we’re going to need:

  • Font is Segoe WP Semibold 64 pt.
  • Vertical center of text container should be at 80 pixels from the top of the image.
  • Total height of usable area is 160. (This leaves room for the tile’s title. If you’re not using a title, you can use the entire height of 173 pixels)

User Control

As I said, we want to use these details and arrange the elements visually. To this end, we’re going to use a custom UserControl and use the XAML markup to specify how our tile works. I have a stand-in transparent png image to use as a glyph next to the numbers. Here’s the UserControl XAML:

[sourcecode language=“xml”] <UserControl x:Class="MySuperTile.DynamicTile" xmlns="" xmlns:x="" xmlns:d="" xmlns:mc="" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" d:DesignHeight="173" d:DesignWidth="173">

    &lt;Canvas Background=&quot;{Binding Background}&quot; Width=&quot;173&quot; Height=&quot;173&quot; x:Name=&quot;layoutRoot&quot;&gt;
        &lt;StackPanel Width=&quot;173&quot; Height=&quot;160&quot; Orientation=&quot;Horizontal&quot;&gt;
            &lt;Image Source=&quot;/images/coffee-cup.png&quot; Width=&quot;58&quot; Height=&quot;98&quot; HorizontalAlignment=&quot;Center&quot; Margin=&quot;12,0,0,0&quot; /&gt;
            &lt;TextBlock x:Name=&quot;numberText&quot; Text=&quot;{Binding Text}&quot; Width=&quot;103&quot; FontFamily=&quot;Segoe WP Semibold&quot; FontSize=&quot;64&quot; TextAlignment=&quot;Center&quot; HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;/&gt;

</UserControl> [/sourcecode]

The user control contains a canvas that contains a stack panel. Note that the stack panel is only 160px tall. Then, the transparent PNG is placed first with the numerals next to it. That’s all we need for the XAML. I used sweet data binding to dynamically set the values of the background and text. You could easily make the image bindable to any image if you want to change it on the fly.

The code behind for the user control is:

[sourcecode language=“csharp”] public partial class DynamicTile : UserControl { /// <summary> /// Identifies <see cref="TextProperty"/> dependency property. /// </summary> public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DynamicTile), null);

    /// &lt;summary&gt;
    /// Identifies &lt;see cref=&quot;TitleProperty&quot;/&gt; dependency property.
    /// &lt;/summary&gt;
    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(&quot;Title&quot;, typeof(string), typeof(DynamicTile), null);

    /// &lt;summary&gt;
    /// Gets or sets the text displayed in the tile.
    /// This is a dependency property.
    /// &lt;/summary&gt;
    public string Text
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }

    /// &lt;summary&gt;
    /// Gets or sets the title of the tile.
    /// This is a dependency property.
    /// &lt;/summary&gt;
    public string Title
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }

    public DynamicTile()

        DataContext = this;

    /// &lt;summary&gt;
    /// Used to render the contents to a tile
    /// &lt;/summary&gt;
    /// &lt;returns&gt;a &lt;see cref=&quot;StandardTileData&quot;/&gt; with the contents of this control&lt;/returns&gt;
    public StandardTileData ToTile()
        // Need to call these, otherwise the contents aren't rendered correctly.
        this.Measure(new Size(173, 173));
        this.Arrange(new Rect(0, 0, 173, 173));

        // The png encoder is the work of the ImageTools API.
        ExtendedImage tileImaged = this.ToImage();


        var p = new PngEncoder();

        const string tempFileName = &quot;/Shared/ShellContent/tileImage.png&quot;;

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            if (myIsolatedStorage.FileExists(tempFileName))

            IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempFileName);
            p.Encode(tileImaged, fileStream);

        var newTile = new StandardTileData
            Title = Title,
            BackgroundImage =
                new Uri(&quot;isostore:&quot; + tempFileName, UriKind.RelativeOrAbsolute)

        return newTile;


In this code, we have a couple of dependency properties (for data binding) and a method to transform the user control into a tile. The difference here is that most guides use the built in JPEG manipulation tools for setting the tile image. But remember I said we are doing a Microsoft style tile with transparency. To get that transparency, we’ll need a PNG image. Saving to PNG is not built into the platform, so I used a 3rd party piece of software called ImageTools ( that will handle the PNG manipulation.

ImageTools provides an extension method “ToImage()” that takes a UIElement and transforms it into an ExtendedImage (which is part of the ImageTools). Then, using that ExtendedImage, we use the PNG encoder to save the image to IsolatedStorage; the image URI is then given to the tile. The other piece of code that is imperative is:

[sourcecode language=“csharp”] this.Measure(new Size(173, 173)); this.Arrange(new Rect(0, 0, 173, 173)); [/sourcecode]

If that’s not in there the elements will render, but not where they should be. So don’t forget those.

Dropping in the Tile

Now, to actually create the tile, we’ll use this code:

[sourcecode language=“csharp”] var mySweetDynamicTile = new DynamicTile { Text = textCount.Text, Title = textCount.Text, Background = new SolidColorBrush(Colors.Purple) };

// Retrieve the contents of the tile as a StandardTileData var newTile = mySweetDynamicTile.ToTile();

// Use the new tile as the primary tile for this app. ShellTile primaryTile = ShellTile.ActiveTiles.First();

if (primaryTile != null) { primaryTile.Update(newTile); } [/sourcecode]

That code instantiates a DynamicTile, sets the title, text, and background color, then asks DynamicTile to create a tile of itself. That tile is then set as the primary tile and, voila, the tile of our app is now set to a dynamic, transparent, tile with Microsoft style numbering. There’s a lot you could do to customize this solution. For example, I used a solid color brush, but there’s nothing stopping you from creating an image brush or a gradient brush. Setting a transparent background (no background, basically), will allow the phone’s accent color to show through.

End Result

Tile Image created by MySuperTile Bam. There you go. No nasty code. Transparent. Beautiful.

A sample project with all the source code is available here.

Comments or Questions? Let me know. Don’t forget to follow me on twitter (@kenstone).