1

I am building a Windows Phone 8 version of an app that I already have for Windows Phone 7.1, which works perfectly. Inside the Application_Deactivated event (in App.xaml.cs) I attempt to update the secondary tile of my app, if pinned to the start screen. Because it is a custom tile, I build it in code using a grid and by adding elements to it. So in the final step, I have something like (layoutRoot is of type Grid) :

layoutRoot.Measure(new Size(336, 336));
layoutRoot.Arrange(new Rect(0, 0, 336, 336));
layoutRoot.UpdateLayout();

WriteableBitmap bitmap = new WriteableBitmap(336, 336);
bitmap.Render(layoutRoot, null);
bitmap.Invalidate();

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (Stream fileStream = storage.CreateFile("/Shared/ShellContent/BackBackgroundImage.png"))
            {
                bitmap.SaveJpeg(fileStream, 336, 336, 0, 100);
            }
        }

So then I can update the Tile very easily. The problem is that when the app is running and I tap the "Windows" button, the app is successfully suspended and the Tile updated, but when I hit "back" to make it active again, a default "loading" text is shown on screen and nothing will happen. However, I have noticed that by commenting out the line bitmap.Render(layoutRoot, null);, the app is re-activated successfully and it works fine, despite the fact that the Tile does not get updated upon suspension, as expected.

In the WP7.1 version of the app, this never happens, although the method for updating the Tile is the same. I really cannot figure out what is going on. Any comment/suggestion/advice will be appreciated.

Edit. Aplication_Deactivated code:

        private void Application_Deactivated(object sender, DeactivatedEventArgs e)
    {
        UpdateLiveTile(); //Generate Tile content (as shown above) and update the Tile if it is pinned
        ExportData(); //Writes data in a file
        StartPeriodicAgent(); //Starts the periodic background agent that updates the live tile
    }
4

2 回答 2

0

解决方法:

RootFrame.Navigating += RootFrame_Navigating;
void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
    if(e.Uri.OriginalString=="app://external/")
    {
        // update the custom tiles here and the resume error is gone..
    }
}

来源:http ://social.technet.microsoft.com/wiki/contents/articles/22256.windows-phone-8-writeablebitmap-and-app-resume.aspx

于 2014-11-02T10:18:08.680 回答
0

我认为您应该考虑将此代码移至页面的 OnNavigatedFrom 事件处理程序。可能会有副作用,因为bitmap.Render需要 UI 线程来呈现您的 xaml 代码。

于 2014-02-22T23:32:48.493 回答