0

I'm attempting to create an image using a xaml control and writable bitmap. I'd say about 95% of the time it works fine while the app is running and have yet to see it fail in the schedule task. When it does fail, the image it creates is odd. All elements align to the top left corner, with a black background (color should be PhoneAccentBrush). I've tried many things to fix this but I'm lost. The only way I can replicate the issue is if I don't call Measure and Arrange. Below is the code, any ideas?

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    TileHelper.UpdateFlipTile(this.LiveTile, this.Facility, this.METARs[0]);
});

public static void UpdateFlipTile(ShellTile shellTile, Facility facility, METAR metar)
{
    /*removed some variables to shorten code*/

    WriteableBitmap mediumImage = TileHelper.CreateMediumTileImage(facility.Description, metar.flight_category, wind, TileHelper.GetUri(metar), temp, metar.observation_time);
    SaveImage(mediumImageFileName, mediumImage);            

    FlipTileData flipTile = new FlipTileData()
    {
        Title = "",
        BackTitle = "",
        BackContent = "",
        WideBackContent = "",
        BackgroundImage = new Uri("isostore:/Shared/ShellContent/" + siteNumberEncoded + ".jpg", UriKind.Absolute)
    };

        shellTile.Update(flipTile);
    }
}

private static WriteableBitmap CreateMediumTileImage(string airport, string flightCategory, string wind, Uri imageUri, string temp, string lastUpdated)
{
    MediumMetarTile tile = new MediumMetarTile(airport.ToLower(), wind, flightCategory, temp, lastUpdated, imageUri);           
    tile.Measure(new Size(336, 336));
    tile.Arrange(new Rect(0, 0, 336, 336));

    WriteableBitmap image = new WriteableBitmap(336, 336);
    image.Render(tile, null);
    image.Invalidate();

    System.Diagnostics.Debugger.Log(0, "TileHelper.CreateMediumTileImage", "medium tile image created");
    return image;
}
Clarke76
  • 724
  • 1
  • 7
  • 17
  • Images on this post (http://stackoverflow.com/questions/14229966/custom-live-tile-rendering-issue-on-windows-phone-7-8?rq=1) look like mine. However I'm currently calling this in the ViewModel once data gets updated. I recently moved it to the ViewModel. It was originally in the OnNavigatedTo override and also has issues when creating a tile when button is clicked. – Clarke76 Oct 22 '13 at 00:34

1 Answers1

0

The issue is because you are using a Grid in your control and the laying out of the grid can take more passes than it gets when not part of a page. (I suspect this is down to a race condition somewhere deep internally in the framework.)

There are two approaches you can take to working around this:

  1. Remove the need for a grid in the XAML
  2. Call UpdateLayout() after both the tile.Measure() and tile.Arrange() calls
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • I was using a Grid as you stated and I actually implemented UpdateLayout yesterday where you stated and so far so good. I could usually get it to bug out when exiting the application quickly and have yet to see the issue after changes. Thanks – Clarke76 Oct 23 '13 at 16:39