我正在開發Windows應用商店應用的銷售應用。我想爲我的產品模塊應用延遲加載。如何將延遲加載應用到Windows Store應用程序中的ListView?
當產品頁面打開時,它從後端獲取產品並顯示在ListBox控件中。 每次加載都需要時間。我認爲主要原因是當我檢查給定的網址上存在的圖像。
這裏是我的代碼和類:
private async Task getAllProductDetails()
{
var resultproductlist = await client.PostAsync(session.Values["URL"] + "/magemobpos/product/getProductList", contents);
if (resultproductlist.IsSuccessStatusCode)
{
string trys = resultproductlist.Content.ReadAsStringAsync().Result;
List<Productlistdata> objProducts = JsonConvert.DeserializeObject<ProductlistResponse>(trys).productlistdata;
Productlistdata Product;
//all product are in objProducts
foreach (var item in objProducts)
{
bool imageexist = false;
//check if image exist on given url or not
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(item.image.ToString()));
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;
}
}
catch (Exception)
{
imageexist = false;
}
//if image not exist, it get default image
if (item.image.ToString().ToLower().Equals("n/a") || imageexist == false)
{
item.image = "Images/NoDataImages/ico-no-orders.png";
}
Product = new Productlistdata()
{
image = item.image,
name = item.name,
price = item.price,
sku = item.sku,
type = item.type[0],
id = item.id
};
//add all product in lstProduct. lstProduct is ListBox Control
lstProduct.Items.Add(Product);
}
}
}
類:
public class Productlistdata
{
public string id { get; set; }
public string sku { get; set; }
public string name { get; set; }
public string status { get; set; }
public string qty { get; set; }
public string price { get; set; }
public string image { get; set; }
public string type { get; set; }
public string full_productname { get; set; }
}
可有人建議我如何申請延遲加載?我不完全知道,但我認爲它可以應用於列表加載後綁定圖像。
我還沒有在windowsRT或UWP應用程序上工作,但是兩者都基於WPF/Silverlight並且默認情況下會進行延遲加載,在這種情況下問題在於你的模型 – MikeT
@MikeT它不是WPF或Silverlight應用程序。這是Windows Store應用程序。你能否在我的代碼中建議我改變模型並調用'imageexist'? –
Windows Store應用程序使用windowsRT或UWP,具體取決於它們是基於Windows 8還是基於Windows 10,並且它們都使用WPF和Silverlight的變體。我的觀點是,雖然我還沒有具體的知識,你正在使用的變化,但我可以提供一般信息,將希望海爾 – MikeT