委托`System.Func <bool>&#39;不需要`1&#39;参数

时间:2016-07-08 11:31:25

标签: c# .net

当用户从登录活动输入密码时将其发送到登录服务 并等待登录服务中的布尔响应。但它给出上面的语法错误(我在代码中提到它)可以解决我的问题。 我要等到服务响应进入我的登录活动

1.登录界面

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RetailAppShared
{
    public interface ILoginServices
    {
        bool AuthenticateUser (string passcode, Func<bool> function);
    }
}

2.登录服务

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using RestSharp;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace RetailAppShared
{
    public class LoginServices : ILoginServices
    {               
        public bool AuthenticateUser (string passcode, Func<bool> function)
        {

            try {
                RestClient _client = new RestClient ("https://www.example.com/app/class");
                var request = new RestRequest ("loginservice", Method.POST) {
                    RequestFormat = DataFormat.Json
                };
                var obj = new login ();
                obj.passcode = passcode;

                request.AddJsonBody (obj);
                request.AddHeader ("Content-type", "application/json");
                _client.ExecuteAsync (request, response => {
                    if (response.StatusCode == HttpStatusCode.OK) {
                        if (!string.IsNullOrEmpty (response.Content)) {
                            var objlog = JsonConvert.DeserializeObject<LoginModel> (response.Content);
                            flag = objlog.result.state == 1 ? true : false;

                            function (true);//error
                        } else
                            flag = false;                            
                    }

                });
            } catch (Exception ex) {
                Debug.WriteLine (@" ERROR {0}", ex.Message);
            }               
        }    

    }

    class login
    {
        public string passcode { get; set; }    
    }
}

3.login活动

Login.Click += async (object sender, EventArgs e) => {

    progress = ProgressDialog.Show (this, "", "Connecting...");

    var isLoginSuccessful = loginAuthenticator.AuthenticateUser                
    (password.Text, (value) => {
        Console.WriteLine (value);
    });//error

    if (progress != null) {
        progress.Dismiss ();
        progress = null;
    }

    if (isLoginSuccessful) {                
        StartActivity (typeof(Ledger_HomeActivity));
        this.Finish ();
    } else {
       Toast.MakeText (this, "Invalid Login credentials! try    again", ToastLength.Short).Show ();
    }
};

2 个答案:

答案 0 :(得分:3)

看起来function表示使用truefalse调用的回调方法,并且不返回任何值您。在这种情况下,您应该Action<bool>而不是Func<bool>,因为Func<bool>执行相反的操作 - 它不需要参数,并会向您返回bool值。

答案 1 :(得分:1)

错误出现在此行

var isLoginSuccessful = loginAuthenticator.AuthenticateUser (password.Text, (value) => { Console.WriteLine (value); }):

Func<bool>返回一个布尔值而不期待任何东西。你想要的是Action<bool>,它需要一个布尔值但返回void

public bool AuthenticateUser (string passcode, Action<bool> function)
{
    function(true);
}