是否可以使用Task< bool>在有条件的情况下? [英] Is it possible to use Task<bool> in if conditions?
问题描述
在Windows Phone 8中,我有方法public async Task<bool> authentication()
.该函数的返回类型为bool
,但是当我尝试在if
条件错误中使用其返回值时,无法将Task<bool>
转换为bool
.
In Windows Phone 8 I have method public async Task<bool> authentication()
. The return type of the function is bool
but when I tried to use its returned value in a if
condition error says can not convert Task<bool>
to 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
关键字:
The return type of your function is Task<bool>
, not bool
itself. To get the result, you should use await
keyword:
bool result = await authentication();
您可以阅读本 MSDN文章,以进一步了解async / await
语言功能.
You can read "What Happens in an Async Method" section of this MSDN article to get more understanding on async / await
language feature.
这篇关于是否可以使用Task< bool>在有条件的情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!