[英]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.