如何跳过检查图像可在每次Windows商店应用程序的URL上找到?

时间:2021-11-20 16:54:47

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

我正在为Windows和平板电脑的Windows商店应用程序工作。我需要列出产品。

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

我想设置一个条件:如果URL上没有产品图像,那么它应该使用默认图像。

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.

这里,产品可以是100或更多。所以,每次它都要求检查服务器。

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

1-如何在C#/ ASP.NET中的http://someurl/myimage.jpg中检查图像是否存在

2- asp.net check if imageURL exists

2- asp.net检查imageURL是否存在

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

3-测试C#中是否存在图像

Please suggest utilized way.

请建议使用方式。

1 个解决方案

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

您可以尝试在模型类中使用INotifyProperty,然后将source设置为ObservableCollection。填充图像可以在绑定列表后以异步方式运行,并在加载的图像(服务器响应)上调用NotifyProperty。

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

您可以尝试在模型类中使用INotifyProperty,然后将source设置为ObservableCollection。填充图像可以在绑定列表后以异步方式运行,并在加载的图像(服务器响应)上调用NotifyProperty。