简体   繁体   English

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

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

I am building a Windows Phone 8 version of an app that I already have for Windows Phone 7.1, which works perfectly. 我正在为Windows Phone 7.1构建一个我已经拥有的应用程序的Windows Phone 8版本,该版本运行良好。 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. Application_Deactivated事件中(在App.xaml.cs ),如果固定到“开始”屏幕,则尝试更新应用的辅助磁贴。 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的类型为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. 因此,我可以非常轻松地更新Tile。 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. 问题是,当该应用程序运行且我点击“ Windows”按钮时,该应用程序已成功挂起并更新了Tile,但是当我单击“后退”使其再次处于活动状态时,默认的“正在加载”文本显示在屏幕,什么也不会发生。 However, I have noticed that by commenting out the line bitmap.Render(layoutRoot, null); 但是,我注意到,通过注释掉了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. ,该应用程序已成功重新激活,并且可以正常运行,尽管Tile并没有如预期的那样进行更新。

In the WP7.1 version of the app, this never happens, although the method for updating the Tile is the same. 在该应用程序的WP7.1版本中,尽管更新Tile的方法是相同的,但这从未发生。 I really cannot figure out what is going on. 我真的不知道发生了什么事。 Any comment/suggestion/advice will be appreciated. 任何意见/建议/建议将不胜感激。

Edit. 编辑。 Aplication_Deactivated code: 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
    }

I think that you should consider moving this code to OnNavigatedFrom event handler of the page(s). 我认为您应该考虑将此代码移至页面的OnNavigatedFrom事件处理程序。 There can be side effects, because bitmap.Render require UI thread to render your xaml code. 可能会有副作用,因为bitmap.Render需要UI线程来呈现您的xaml代码。

Workaround: 解决方法:

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..
    }
}

Source: http://social.technet.microsoft.com/wiki/contents/articles/22256.windows-phone-8-writeablebitmap-and-app-resume.aspx 来源: 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.

相关问题 在application_deactivated事件WP7中取消backgroundworker - Cancelling backgroundworker on application_deactivated event WP7 什么时候Application_Deactivated **不**运行? - When is Application_Deactivated **not** run? 检测并重新激活我的应用(如果正在运行)-紧凑型框架 - Detect and re-activate my app if running - compact framework WCF:NetMsmq而不是Http的应用程序池挂起后,自定义ServiceHostFactory错误 - WCF: Custom ServiceHostFactory errors after Application Pool suspension for NetMsmq but not Http Template10在应用暂停后恢复运行服务 - Template10 Resume running services after app suspension WP7:从墓碑状态恢复应用程序导致应用程序崩溃。 使用Writeablebitmap - WP7: Bringing Back App from a Tombstone State Causes App to Crash. Using a Writeablebitmap WP7-停用应用程序并重新激活后,Navigaton停止工作 - WP7 - Navigaton stops working after the application is deactivated and reactivated 在Task中引发事件会导致订阅者方法失败 - Raising an event in a Task causes the subscriber method to fail 无法在 Unity 中激活先前停用的游戏对象(画布) - Unable to activate a previously deactivated gameObject (canvas) in Unity 如何禁用UWP App暂停? - How to Disable UWP App Suspension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM