异步/等待不工作”;字符串不包含GetAwaiter的定义“”;并且没有扩展

本文关键字:扩展 定义 包含 等待 工作 字符串 GetAwaiter 异步 | 更新日期: 2023-09-27 17:59:43

我正在Visual Studio Xamarin中制作Android应用程序,当通过Web服务与数据库建立连接以验证密码时,在应用程序的登录屏幕中,我想显示进度对话框,直到建立连接。

这是我在客户端的代码:

private string login()
{
    string x = null;
    var progress = ProgressDialog.Show(this, "waiting", "Loading");
    progress.SetProgressStyle(ProgressDialogStyle.Spinner);
    new Thread(new ThreadStart(delegate
    {
        RunOnUiThread(async () =>
        {
            x = await services.Verification("abc", "xyz");
            progress.Dismiss();
        });
     })).Start();
    return x;
}

在服务器端:

public string Verification(string userName, string password)
{
    SqlConnection conn = new SqlConnection(@"");
    conn.Open();
    string query = "select category from ACCOUNTS where loginId = '" + userName + "' and pasword= '" + password + "'";
    SqlCommand cmd = new SqlCommand(query);
    cmd.Connection = conn;
    string catagory = null;
    SqlDataReader account = cmd.ExecuteReader();
    if (account.HasRows)
    {
        if (account.Read())
        {
            catagory = account[0].ToString();
        }
    }
    conn.Close();
    return catagory;
}

以下是第x = await services.Verification("abc", "xyz");行的login()函数中的错误,它显示:

 'String' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'String' could be found (are you missing a using directive or an assembly reference?)

异步/等待不工作”;字符串不包含GetAwaiter的定义“”;并且没有扩展

要使用这个:

x = await services.Verification("abc", "xyz");

您必须使用API,如:

public async Task<string> Verification(string userName, string password)

但是,您使用什么与数据库进行通信?看起来你是直接调用Verification方法,而不是通过REST调用服务。当你部署应用程序时,直接调用该方法是不起作用的。。。您需要使用具有async/await、RestSharp或本教程中定义的其他功能的HttpClient来调用web服务的代理:https://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/.

取自此处:

var httpClient = new HttpClient();
Task<string> contentsTask = httpClient.GetStringAsync("http://xamarin.com"); 
string contents = await contentsTask;