gpt4 book ai didi

c# - 是否可以在 if 条件下使用 Task

转载 作者:IT王子 更新时间:2023-10-29 04:47:10 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com