Windows Phone(7/8)上自定义磁贴渲染问题

5

在我的Windows Phone应用程序的主页上,用户可以点击一个按钮来执行一些操作,从而触发Live Tile更新。

我遇到的问题是,如果用户点击按钮然后非常快地按下手机的返回按钮,有时候Live Tile就无法正确渲染。这个问题很少发生,但它确实会发生,当它发生时看起来很糟糕...

enter image description here

我实现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"));

我认为引起这一切的原因是,当用户过快地点击“返回”按钮时,操作系统会终止应用程序中正在运行的所有进程,并且此时渲染尚未完成。我在思考是否有一种方法可以知道何时完成渲染,以便取消“返回”按钮并等待直到完成后再手动退出应用程序。但我不知道如何做...任何对此的帮助都将不胜感激!
1个回答

5

我在我的WP8应用程序中遇到了类似的问题。问题是我在ApplicationDeactivated事件处理程序中更新了我的Tile。问题在于你不应该在那里更新你的Tile,而是应该在你的MainPage.OnNavigatedFrom重写中更新。一旦我改变了这个,它就可以正常工作了。


我实际上是在用户点击按钮后立即在视图模型中调用更新。但我不确定它是否适用于OnNavigatedFrom。我会尝试一下并让您知道,谢谢! - Justin XL
1
如果您正在ViewModel中更新磁贴,除非您实现一些关闭通知机制,否则您无法控制应用程序是否同时关闭。在OnNavigatedFrom中更新应该是安全的方式。 - Martin Suchan
1
我觉得你是对的!不过有一件事我注意到了,每次我创建一个新的用户控件实例时,我都必须调用UpdateLayout才能使它正确渲染。你的回答帮助我找到了正确的方法。非常感谢你!:) - Justin XL
1
+1. 这是一个已记录的 WP7->WP8 怪异现象。"在 Windows Phone 8 中,在 Closing 处理程序中使用 Create(Uri, ShellTileData) 方法创建 Tile 将抛出 InvalidOperationException 异常。" @ http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206947(v=vs.105).aspx - JustinAngel
谢谢@JustinAngel,链接非常有用! - Justin XL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接