我是Xamarin Android的新手,我正在开发用于从服务器获取和显示图像的简单应用程序。 这是我的代码 -
public void testWCF2()
{
var imgView = FindViewById<ImageView>(Resource.Id.imageView123);
using(var bm = await GetImageFromUrl(@"http://xamarin.com/content/images/pages/forms/example-app.png")) //At this line an error is showing 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'.
imgView.SetImageBitmap(bm);
}
private async Task<Bitmap> GetImageFromUrl(string url)
{
using(var client = new HttpClient())
{
var msg = await client.GetAsync(url);
if (msg.IsSuccessStatusCode)
{
using(var stream = await msg.Content.ReadAsStreamAsync())
{
var bitmap = await BitmapFactory.DecodeStreamAsync(stream);
return bitmap;
}
}
}
return null;
}
有人请帮帮我。
由于
答案 0 :(得分:4)
您需要在方法中添加async
修饰符,如:
public async void testWCF2()
{
var imgView = FindViewById<ImageView>(Resource.Id.imageView123);
using(var bm = await GetImageFromUrl(@"http://xamarin.com/content/images/pages/forms/example-app.png"))
imgView.SetImageBitmap(bm);
}
MSDN说:
使用async修饰符指定方法lambda表达式或匿名方法是异步的。如果在方法或表达式上使用此修饰符,则将其称为异步方法。