在Application_Deactivated事件中呈现WriteableBitmap会导致应用程序在暂停后无法重新激活

时间:2014-02-22 21:04:12

标签: c# windows-phone-8 live-tile tombstoning

我正在为Windows Phone 7.1构建一个Windows Phone 8版本的应用程序,它可以很好地运行。 在Application_Deactivated事件内(在App.xaml.cs中)我尝试更新应用的辅助磁贴,如果固定到开始屏幕。因为它是一个自定义磁贴,所以我使用网格在代码中构建它并向其中添加元素。所以在最后一步中,我有类似{layoutRoot类型为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);
            }
        }

那么我可以很容易地更新Tile。问题是,当应用程序运行时,我点击“Windows”按钮,应用程序成功暂停并更新了Tile,但当我点击“返回”使其再次处于活动状态时,默认的“加载”文本显示在屏幕,什么都不会发生。但是,我注意到,通过注释掉行bitmap.Render(layoutRoot, null);,该应用程序已成功重新激活,并且工作正常,尽管事先没有像预期的那样在暂停时更新Tile。

在应用程序的WP7.1版本中,这种情况永远不会发生,尽管更新Tile的方法是相同的。我真的无法弄清楚发生了什么。任何评论/建议/建议将不胜感激。

编辑。 Aplication_Deactivated代码:

        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
    }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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