gpt4 book ai didi

c# - 每次 Windows 商店应用程序如何跳过以检查 URL 上的图像是否可用?

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:41 24 4
gpt4 key购买 nike

我正在开发适用于 Windows 和平板电脑的 Windows 商店应用程序。我需要列出产品。

我想设置一个条件:如果产品图片在 URL 上不可用,那么它应该使用默认图片。

代码如下:

//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
}
}

在这里,product 可以是 100 或更多。所以,每次它调用检查服务器。

有没有其他方法可以检查,因为它每次都会调用检查?它消耗时间。

我引用了以下链接。但在所有情况下,我每次都需要打电话来检查图像是否存在。

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#

请建议使用的方式。

最佳答案

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

关于c# - 每次 Windows 商店应用程序如何跳过以检查 URL 上的图像是否可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36638511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com