2

I am working on Windows store app for windows and tablet. I need to list the product.

I want to set one condition: If product image is not available on URL, then it should use default image.

Here is the code:

//get product list by service call and bind to productList.
foreach(product item in productList)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(item.image.ToString()));
    bool imageexist = false;
    using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
    {
        int imagelength = Convert.ToInt32(response.ContentLength);
        if (imagelength > 0)
        {
            imageexist = true;
        }
        else
        {
            imageexist = false;
        }
    }

    if (item.image == "N/A" || imageexist == false)
    {
        item.image = "Images/NoDataImages/ico-no-orders.png";       //default image
    }
}

Here, product can be 100 or more. So, every time it call to check on server.

Is there any other way to check because it calls to check everytime? It consume times.

I have referred following links. But in all cases I need to call to check image exist everytime.

1- How can I check if an Image exists at http://someurl/myimage.jpg in C#/ASP.NET

2- asp.net check if imageURL exists

3- Test to see if an image exists in C#

Please suggest utilized way.

4
  • Why don't you rely on the service response - item.image == "N/A". If the server responded "N/A" in you case, it means the image is not available. Are you in control of the server part? Maybe you can move the logic to your image control - if a provider url is not available or any other error , it can show the default image. Commented Apr 15, 2016 at 7:46
  • @SergeyL It doesn't return N/A in all cases. Sometimes it provide URL and it may happened that image is not there on URL. This is the case. Commented Apr 15, 2016 at 8:26
  • Could you handle it in the control that displays the image? Commented Apr 15, 2016 at 8:38
  • @SergeyL I can but I bind first in list and then bind list assign to ListControl.DataSource. Commented Apr 15, 2016 at 9:18

1 Answer 1

0

You can try to use INotifyProperty in your model class and then, set source as ObservableCollection. Filling images can run in async method after bind list and call NotifyProperty changed on image loaded (server response).

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please suggest me link which can provide some programming idea?
link There is example ;) or public class Model : INotifyPropertyChanged { public SomeBindedProperty{get;} public SomeProperty{get;set{ SomeBindedProperty = value; NotifyPropertyChanged("SomeBindedProperty");} public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string propertyName) {if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.