在我的Windows Phone应用程序的主页上,用户可以点击一个按钮来执行一些操作,从而触发Live Tile更新。
我遇到的问题是,如果用户点击按钮然后非常快地按下手机的返回按钮,有时候Live Tile就无法正确渲染。这个问题很少发生,但它确实会发生,当它发生时看起来很糟糕...
我实现Live Tile的方式是,创建一个与Live Tile完全相同的用户控件,然后将其保存到隔离存储中。然后检索它并将其存储在FliptileData对象中。最后我调用ShellTile的Update方法。请参见以下代码片段以演示该过程。
// the function that saves the user control to isolated storage
public Uri SaveJpegToIsolatedStorage(FrameworkElement tile, string suffix, int tileWidth = 336, int tileHeight = 336)
{
var bmp = new WriteableBitmap(tileWidth, tileHeight);
// Force the content to layout itself properly
tile.Measure(new Size(tileWidth, tileHeight));
tile.Arrange(new Rect(0, 0, tileWidth, tileHeight));
bmp.Render(tile, null);
bmp.Invalidate();
// Obtain the virtual store for the application
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(IsolatedStorageFileName + suffix, FileMode.Create, myStore))
{
try
{
bmp.SaveJpeg(fileStream, tileWidth, tileHeight, 0, 100);
}
catch (Exception)
{
return null;
}
}
return new Uri("isostore:/" + IsolatedStorageFileName + suffix, UriKind.Absolute);
}
// save the user control to isolated storage and prepare the FlipTileData object
wideFrontTileImage = SaveJpegToIsolatedStorage((UserControl)this.WideFrontTile, "_wide_front", 691);
var flipTileData = new FlipTileData();
flipTileData.WideBackgroundImage = wideFrontTileImage;
return flipTileData;
// update the live tile
var shellTile = ShellTile.ActiveTiles.FirstOrDefault();
shellTile.Update(customTile.GetFlipTileData(data.UndoneMemosCount == "0" && data.TotalMemosCount == "0"));
我认为引起这一切的原因是,当用户过快地点击“返回”按钮时,操作系统会终止应用程序中正在运行的所有进程,并且此时渲染尚未完成。我在思考是否有一种方法可以知道何时完成渲染,以便取消“返回”按钮并等待直到完成后再手动退出应用程序。但我不知道如何做...任何对此的帮助都将不胜感激!