是否可以使用Task< bool>在有条件的情况下? [英] Is it possible to use Task<bool> in if conditions?

查看:555
本文介绍了是否可以使用Task< bool>在有条件的情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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&lt; bool&gt;在有条件的情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆