是否可以在条件下使用Task <bool>?

Moh*_*bas 18 c# task-parallel-library async-await c#-5.0 windows-phone-8

在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;
}
Run Code Online (Sandbox Code Playgroud)

Ser*_*diy 36

函数的返回类型Task<bool>不是bool它本身.要获得结果,您应该使用await关键字:

bool result = await authentication();
Run Code Online (Sandbox Code Playgroud)

您可以阅读本MSDN文章中的"异步方法中发生的事情"部分,以便更好地理解语言功能.async / await