创建ShellTile映像问题

时间:2013-10-22 00:23:12

标签: c# windows-phone-8

我正在尝试使用xaml控件和可写位图创建图像。我说大约95%的时间它在应用程序运行时工作正常并且尚未在计划任务中看到它失败。当它失败时,它创建的图像是奇数。所有元素都对齐左上角,背景为黑色(颜色应为PhoneAccentBrush)。我已经尝试了很多东西来解决这个问题,但我迷路了。我可以复制问题的唯一方法是,如果我不打电话给测量和安排。以下是代码,任何想法?

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;
}

1 个答案:

答案 0 :(得分:0)

问题是因为您在控件中使用了网格,并且网格的布局可能比不是页面的部分时需要更多的传递。 (我怀疑这是在框架内部深处的竞争条件。)

您可以采取两种方法来解决这个问题:

  1. 在XAML中删除对网格的需求
  2. UpdateLayout()tile.Measure()来电
  3. 之后致电tile.Arrange()