c# - 是否可以在 if 条件下使用 Task<bool> ?

标签 c# windows-phone-8 task-parallel-library async-await c#-5.0

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

相关文章:

c# - Visual Studio 2010 IntelliSense 窗口文本输入字段

c# - 如何读取和处理属于一起的多行?

c# - 具有透明背景的 Webbrowser 控件

windows - 在 hyper-v 虚拟机上完成 Windows 应用程序开发

c# - 使用 MVVM 模式设计需要 BackgroundAgent 的应用程序的最佳实践

c# - WCF 数据服务与 WCF 服务库

c# - Caliburn.micro View ViewModel 名称解析问题

c# - 在 Tpl 中处理异常

c# - TaskFactory 内部任务永远不会被执行并且始终处于 WaitingForActivation 状态

wpf - 为什么我的 GUI 卡住?