开发者

How to check if Image is loaded while coming back in Navigation stack

In my MainPage I have added many images, each of them is retrieved from server by making HttpWebRequest. Also there are some links on MainPage, clicking on those links takes the user to a new page.

The problem I am facing is before all the images are loaded in the MainPage itself, if I click on any of the links on the MainPage, a new page is created and shown and all the pending image requests are canceled. Now If I come back to MainPage by clicking on the back button, the unloaded images still remain unloaded.

I have no idea how to check whether the images have already been loaded or not, when I come back to MainPage in the navigation stack(If I am able to find out that some images are not loaded, I can request the se开发者_StackOverflowrver again for the unloaded images). I need a generic solution for this. Because parent child relationship in the MainPage content layout is dynamic, means I do not know the hierarchy of the Image control, Image control can be a child of a canvas which in turn added to the MainPage's Content grid or the image can be added in one of the sections of a panorama/pivot which in turn added to the MainPage's Content grid, etc,....


There is an event on the Image control related to that -> http://msdn.microsoft.com/en-us/library/system.windows.controls.image.imageopened(v=vs.95).aspx


  1. As argh suggested use the ImageOpened event (and perhaps also ImageFailed) to track whether the image has been loaded successfully. You could create a wrapper control around Image (can't inherit since it is sealed) to monitor the event and implement your own IsLoaded property.
  2. You can use VisualTreeHelper.GetParent to traverse up through the tree and see whether the Image control is a descendant from MainPage for the second part of your problem.

This should work, however, I'm wondering why the images are not re-loaded when you press the back button. I don't think any of the suggested 'hacks' should be necessary. Are you using any custom logic for image loading etc.?


You can check Name of Image Temp . beacause in my project i use image temp show before image has load in server.

You can check the current name of the Image, if it is similar to the Temp Image name, then continue down they will not down My code :

if(((BitmapImage)img.Source).UriSource.Equals("/NhomMua;component/Image/img_temp_sale.png"))
                        {
                           //code of you 
                        }


I would suggest you have a look at the LowProfileImageLoader http://blogs.msdn.com/b/delay/archive/2010/09/02/keep-a-low-profile-lowprofileimageloader-helps-the-windows-phone-7-ui-thread-stay-responsive-by-loading-images-in-the-background.aspx) as a generic way to handle loading of images in an efficient manner. If you specifically need to be able to cancel and resume the loading of images you should able to modify that code to handle that pretty easily - although I'm not sure why you'd want to, remember its more efficient on the battery to download all the images in one hit than to stop and start.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜