Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a live tile for WP8 with dynamic image from control throws a "NotSupportedException"

I'm using the following code to take a custom user control, make a bitmap out of it, and then save it to isolated storage for the purposes of a WP8 Live Tile.

public static void UpdateTile()
{
    var frontTile = new LiveTileRegular(); // Custom Control
    frontTile.Measure(new Size(173, 173));
    frontTile.Arrange(new Rect(0, 0, 173, 173));

    var bmp = new WriteableBitmap(173, 173);
    bmp.Render(frontTile, null);
    bmp.Invalidate();

    const string filename = "/LiveTiles/LiveTileRegular.jpg";

    using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!isf.DirectoryExists("/LiveTiles"))
        {
            isf.CreateDirectory("/LiveTiles");
        }

        using (var stream = isf.OpenFile(filename, FileMode.OpenOrCreate))
        {
            bmp.SaveJpeg(stream, 173, 173, 0, 100);
        }

        Debug.WriteLine("Image Exists: " + (isf.FileExists(filename) ? "Yes" : "No")); // Displays "Yes"
    }

    ShellTile.ActiveTiles.First().Update(new FlipTileData
    {
        Title = "Title",
        BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute),
    }); // Throws a NotSupportedException
}

The NotSupportedException gets thrown on the ShellTile.ActiveTiles.First().Update() method with very non-descriptive messaging.

Is there something that I'm obviously doing wrong?

like image 531
Mike Richards Avatar asked Dec 21 '22 08:12

Mike Richards


1 Answers

The "TargetInnvocationException" exception was actually hiding the underlying problem of a "NotSupportedException" exception which I found after moving ShellTile.ActiveTiles.First().Update() out of the UI thread.

The exception was still not descriptive as to what the problem was, but after some rummaging through different forums and documentation, I found that the path of the dynamically created image is very important when using it with Live Tiles.

If you are using an image in isolated storage for the purpose of the live tile or shell tile, then the base folder must be

/Shared/ShellContent

After changing

const string filename = "/LiveTiles/LiveTileRegular.jpg";

to

const string filename = "/Shared/ShellContent/LiveTileRegular.jpg";

Everything worked fine.

Could us Windows Phone developers get better exception messaging?!? :)

like image 167
Mike Richards Avatar answered Dec 31 '22 15:12

Mike Richards