【问题标题】:Delegate `System.Func<bool>' does not take `1' arguments委托 `System.Func<bool>' 不接受 `1' 参数
【发布时间】:2016-07-08 11:31:24
【问题描述】:

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

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.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 ();
    }
};

【问题讨论】:

  • A Func&lt;bool&gt; 类似于返回 bool 但没有参数的方法。你应该把它叫做bool result = function();。相反,如果您想传入 bool 并且没有返回值,那么您应该改用 Action&lt;bool&gt;

标签: c# .net


【解决方案1】:

看起来function 代表一个回调方法,使用truefalse 的值调用它,并且不会返回任何值给您。在这种情况下,您应该使用Action&lt;bool&gt; 而不是Func&lt;bool&gt;,因为Func&lt;bool&gt; 的作用正好相反——它不接受任何参数,并返回一个bool 值给您。

【讨论】:

    【解决方案2】:

    错误出现在这一行

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 2017-04-05
      • 1970-01-01
      相关资源
      最近更新 更多