C# 在Xamarin Monotouch的uiimageview中异步加载图像
我有一个表格单元格,因为我在scrollview中水平显示多个图像,我需要将图像异步加载到imageview中,这意味着如果没有加载图像,我需要放置默认的placefolder.png文件 如果加载了图像,我需要用加载的图像替换placeholder.png 谁能帮帮我吗C# 在Xamarin Monotouch的uiimageview中异步加载图像,c#,asynchronous,xamarin.ios,xamarin,C#,Asynchronous,Xamarin.ios,Xamarin,我有一个表格单元格,因为我在scrollview中水平显示多个图像,我需要将图像异步加载到imageview中,这意味着如果没有加载图像,我需要放置默认的placefolder.png文件 如果加载了图像,我需要用加载的图像替换placeholder.png 谁能帮帮我吗 _image = new UIImageView(FromUrl(contentItem.FilePath, cachedImages)) { Tag = 1001 }; _imagePath = contentItem.Fi
_image = new UIImageView(FromUrl(contentItem.FilePath, cachedImages)) { Tag = 1001 };
_imagePath = contentItem.FilePath;
_image.Frame = new RectangleF(x, 0, 250, _image.Image.CGImage.Height);
x = x + (Convert.ToInt32( Bounds.Width));
scrollwidth = scrollwidth + Convert.ToInt32( Bounds.Width);
_scrollView.ContentSize = new SizeF (scrollwidth, _image.Image.CGImage.Height);
_scrollView.Add (_image);
由于它是一个表格单元格,您无法确定任务完成时单元格是否仍然相同,因此我将创建一个类似于以下内容的下载任务:
public async static Task<string> DownloadImage(string fileWebAddress, string localPath)
{
await new WebClient ().DownloadFileTaskAsync (url, fileName);
return localPath;
}
如果不是,则启动异步下载,当它返回时,检查任务是否针对当前单元格(contentItem不能是本地函数变量,它需要是可以更改的单元格变量):
您从哪里加载图像?这将决定您可以调用哪种异步方法来检索它们
if (File.Exist(contentItem.FilePath))
{
SetImage(_image, contentItem.FilePath);
}
else
{
DownloadImage(contentItem.WebPath, contentItem.FilePath).ContinueWith (t =>
{
if (contentItem.FilePath == t.Result)
{
SetImage(_image, t.Result);
}
});
}