在我的 Windows Phone 应用程序的主页上,用户可以单击一个按钮来执行一些操作,这将触发动态磁贴更新。
我遇到的问题是,如果用户单击按钮,然后非常快速地按下手机的后退按钮,则实时磁贴有时无法正确呈现。这个问题很少发生,但它确实发生了,当它发生时,它看起来很糟糕......
我实现动态磁贴的方法是,创建一个看起来与动态磁贴完全相同的用户控件,然后将其保存到独立存储中。然后检索它并将其存储在 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"));
我认为导致这一切的原因是,当用户过快地单击“后退”按钮时,操作系统会终止应用程序中运行的所有进程,并且当时未完成渲染。我在想是否有办法知道渲染何时完成,所以我可以取消后退按钮并等到它完成然后手动退出应用程序。但我就是不知道怎么...
对此的任何帮助将不胜感激!
最佳答案
我在我的 WP8 应用程序中遇到了类似的问题。问题是我正在 ApplicationDeactivated 事件处理程序中更新我的 Tile。问题是你不应该在那里更新你的瓷砖,而是在你的 MainPage.OnNavigatedFrom 覆盖中。一旦我改变了这个,它就可以正常工作。
关于windows-phone-7 - Windows Phone 上的自定义实时磁贴渲染问题 (7/8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14229966/