委托 'System.Func' 不接受 '1' 参数

本文关键字:不接受 参数 Func System 委托 bool | 更新日期: 2023-09-27 17:56:04

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

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

委托 'System.Func<bool>' 不接受 '1' 参数

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

错误出现在此行

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