【发布时间】:2024-04-11 07:20:02
【问题描述】:
我是 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;
}
请有人帮帮我。
谢谢
【问题讨论】:
-
您在非
async方法中调用await GetImageFromUrl()testWCF2()
标签: c# android web-services xamarin async-await