【发布时间】:2016-12-16 16:23:52
【问题描述】:
我正在尝试从互联网上异步下载一些图片。我已经构建了GetImageBitmapFromUrl 方法如下
async Task<Bitmap> GetImageBitmapFromUrl(string url)
{
Bitmap imageBitmap = null;
try
{
using (var webClient = new WebClient())
{
var imageBytes = await webClient.DownloadStringTaskAsync(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = BitmapFactory.DecodeByteArray(Encoding.ASCII.GetBytes(imageBytes), 0, imageBytes.Length);
}
}
}
catch
{
//Silence is gold.
}
return imageBitmap;
}
我现在尝试在我的 setter 中调用这个方法
List<string> _pictures;
Bitmap[] imageBitmap;
int currentPic = 0;
ImageView gellaryViewer;
public List<string> pictures
{
set
{
if (value.Count == 0)
{
gellaryViewer.Visibility = ViewStates.Gone;
}
else
{
gellaryViewer.Visibility = ViewStates.Visible;
_pictures = value;
currentPic = 0;
imageBitmap = new Bitmap[value.Count];
for (int i = 0; i < value.Count; i++)
//The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
imageBitmap[i] = await GetImageBitmapFromUrl(value[i]);
displayPic();
}
}
get { return _pictures; }
}
但我收到此错误The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
如何使用“异步”修饰符标记设置器?
【问题讨论】: