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.

Community
  • 1
  • 1
Nanji Mange
  • 2,155
  • 4
  • 29
  • 63
  • 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. – Sergey L Apr 15 '16 at 07: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. – Nanji Mange Apr 15 '16 at 08:26
  • Could you handle it in the control that displays the image? – Sergey L Apr 15 '16 at 08:38
  • @SergeyL I can but I bind first in list and then bind list assign to ListControl.DataSource. – Nanji Mange Apr 15 '16 at 09:18

1 Answers1

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).

  • Can you please suggest me link which can provide some programming idea? – Nanji Mange Apr 15 '16 at 12:03
  • [link](http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist) 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)); } } } – Elektryczny Apr 15 '16 at 12:23