How to check if Image is loaded when it returns to the navigation stack

In my MainPage, I added a lot of images, each of which is retrieved from the server, creating an HttpWebRequest. There are also links to MainPage, clicking on these links leads the user to a new page.

The problem I am facing is that all the images are uploaded to MainPage themselves, if I click on any of the links on MainPage, a new page is created and displayed, and all requests for pending images are canceled. Now, if I go back to MainPage by clicking the back button, the uploaded images will still be uploaded.

I don’t know how to check if images are uploaded or not when I return to MainPage in the navigation stack (if I can find out that some images are not uploaded, I can request the server again for the uploaded images). For this I need a general solution. Since the parent-child relationship in the MainPage content layout is dynamic, which means I don’t know the image control hierarchy, the image control can be a descendant of the canvas, which in turn added content to the MainPage grid or images can be added to one pan / rotate section, which , in turn, added to the MainPage Content grid, etc., ....

+4
source share
4 answers
+2
source
  • As Argh suggested using the ImageOpened event (and possibly also ImageFailed ) to keep track of whether the image was uploaded successfully. You can create a wrapper control around the image (it cannot inherit because it is sealed) to control the event and implement its own IsLoaded property.
  • You can use VisualTreeHelper.GetParent to go up the tree and see if the Image control is a descendant of MainPage for the second part of your problem.

This should work, however, I wonder why the images do not load when you click the back button. I do not think that any of the proposed "hacks" should be necessary. Do you use any custom logic to load images, etc.?

+1
source

You can check the name of the tempo of the image. beacause in my project i use the temp image until the image is uploaded to the server.

You can check the current image name if it looks like a temporary image name, and then continue down, they will not. My code:

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

I would suggest you take a look at 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 general way to efficiently manage image loading. If you need to be able to cancel and resume downloading images, you should be able to change this code to handle it quite easily - although I'm not sure why you want, remember that it is more efficient for the battery to download all images in one stroke. than stop and start.

0
source

All Articles