繁体   English   中英

在Application_Deactivated事件中渲染WriteableBitmap会导致应用在暂停后无法重新激活

[英]Rendering a WriteableBitmap within Application_Deactivated event causes app to fail to re-activate after suspension

我正在为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
    }

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

解决方法:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM