因此,我正在尝试编写一个简单的通用应用程序以从网络上获取比特币的价格。我有一个异步方法,我从here得到从一个URL的JSON并将其放入一个字符串。这里就是我所谓的方法:C#Windows通用应用程序未检测到异步方法
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
CoinPriceBackend CP = new CoinPriceBackend();
string response = await GetFromAPI();
}
这是方法:
async Task<string> GetFromAPI()
{
try
{
//Create HttpClient
HttpClient httpClient = new HttpClient();
//Define Http Headers
httpClient.DefaultRequestHeaders.Accept.TryParseAdd("application/json");
//Call
string ResponseString = await httpClient.GetStringAsync(
new Uri("https://api.bitcoinaverage.com/ticker/GBP/"));
//Replace current URL with your URL
return ResponseString;
}
catch (Exception ex)
{
return "ERROR: " + ex;
}
}
我得到的错误
'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'.'
但这种方法是异步...如何我能解决这个问题吗?
谢谢!
*“的‘等待’操作只能在**一个异步方法中使用**。“* - 你的'App'构造函数不是。 – IInspectable