在 Windows Phone 8 中我有方法 public async Task<bool> authentication()
.函数的返回类型是bool
但是当我试图在 if
中使用它的返回值时条件错误说无法转换 Task<bool>
至 bool
.
public async Task<bool> authentication()
{
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string> ("user", _username),
new KeyValuePair<string, string> ("password", _password)
};
var serverData = serverConnection.connect("login.php", pairs);
RootObject json = JsonConvert.DeserializeObject<RootObject>(await serverData);
if (json.logined != "false")
{
_firsname = json.data.firsname;
_lastname = json.data.lastname;
_id = json.data.id;
_phone = json.data.phone;
_ProfilePic = json.data.profilePic;
_thumbnail = json.data.thumbnail;
_email = json.data.email;
return true;
}
else
return false;
}
最佳答案
函数的返回类型是 Task<bool>
, 不是 bool
本身。要获得结果,您应该使用 await
关键词:
bool result = await authentication();
您可以阅读此 MSDN article 的“异步方法中发生的事情”部分了解更多 async / await
语言特征。
关于c# - 是否可以在 if 条件下使用 Task<bool> ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23272911/