Why do my images sometimes reload (and WriteableBitmaps disappear) on resume?

拜拜、爱过 提交于 2019-12-03 16:30:57

I've done some experiments that made it clear that WinRT is just losing the contents of the WriteableBitmap, not the WriteableBitmap itself. The WriteableBitmap instance is still viable; if you give it new pixel data, it works fine. It's just that sometimes, on app resume, its contents get replaced with a swath of transparent pixels.

I've been able to work around this problem by hooking my app's OnResuming event, and using that event to grab my WriteableBitmap's PixelBuffer.AsStream(), write new pixels, and call WriteableBitmap.Invalidate(). If the code that regenerates the image is synchronous, then the image doesn't even flicker when the app resumes.

I suspect that ordinary bitmaps (loaded via a Source URI) are also losing their pixel buffers, and having to reload their pixels after the app resumes. Image loading is async, which explains the delay / flicker before their contents reappear.

I haven't found a way to know whether the image lost its pixels (though obviously URI-loaded images have some way of knowing, since they reload themselves automatically). So with WriteableBitmaps, the safest thing seems to be to always repopulate them on app resume.

To somewhat supplant the previous answer, I've found that simply invalidating the WriteableBitmap should do the trick. I would advocate trying that first before you rewrite the image, as that may have a more substantial impact on your application over just Invalidating it.

This bug can also be reproduced by forcing your application to suspend and resume. It initially was very difficult for us to pin down when it would happen as we were waiting for the application to suspend. This is a link to an article on how to simulate Suspend and Resume in WinRT applications.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!