C# Xamarin跨平台PCL GetStringAsync错误

C# Xamarin跨平台PCL GetStringAsync错误,c#,xamarin,async-await,C#,Xamarin,Async Await,我目前正在学习如何开发跨平台应用程序 我遇到了一只虫子,我不知道它是不是虫子 但当我尝试使用函数GetStringAsync时,如下图所示 这会导致一个错误,有人能帮忙吗 这是密码 protected override async void OnAppearing() { var client = new HttpClient(); string a =await client.GetStringAsync("https://jsonplaceholder.typicode.co

我目前正在学习如何开发跨平台应用程序 我遇到了一只虫子,我不知道它是不是虫子 但当我尝试使用函数
GetStringAsync
时,如下图所示 这会导致一个错误,有人能帮忙吗

这是密码

protected override async void OnAppearing()
{
    var client = new HttpClient();
    string a =await client.GetStringAsync("https://jsonplaceholder.typicode.com/posts");

    base.OnAppearing();
}
对于我通过Nuget Microsoft.Net.Http添加的PCL项目 对于Android项目,我添加了
System.Net.Http

我只是意识到如果你想下载Microsoft.Net.Http 在android项目中,您需要下载Microsoft.bcl.Build
首先,我这么做了,但问题仍然存在。

事件处理程序上允许使用异步void。但是,
OnAppearing
不是事件处理程序。它是页面基类上的虚拟方法。不过好的是,
OnAppearing
方法实际上是在
出现
事件之前调用的

因此,在OnAppearing方法中订阅页面/视图中出现的
事件

protected override void OnAppearing() {
    this.Appearing += Page_Appearing;
}
并在实际的偶数处理程序上调用异步代码,该处理程序在之后调用

HttpClient client = new HttpClient();
private async void Page_Appearing(object sender, EventArgs e) {
    //...call async code here
    var url = "https://jsonplaceholder.typicode.com/posts";
    var a = await client.GetStringAsync(url);

    //unsubscribing from the event
    this.Appearing -= Page_Appearing;
}

您的帖子中没有包含代码或错误消息,我们应该如何帮助您?请不要将它们作为图片发布。