Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin forms命令CanExecute不更新按钮已启用_Xamarin_Xamarin.forms - Fatal编程技术网

Xamarin forms命令CanExecute不更新按钮已启用

Xamarin forms命令CanExecute不更新按钮已启用,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我正在尝试将样式应用于已被命令禁用的按钮 我假设IsEnabled状态是由canexecutechanged事件触发的属性,但似乎不是 哪个按钮属性受到影响?我可以挂接到该事件中,以便为按钮提供样式吗?在viewmodel中,您可以添加一个属性,该属性将导致启用或禁用按钮。下面是一个例子 public Command FacebookLoginCommand { get; set; } private bool _IsBusy; public override bool IsBusy {

我正在尝试将样式应用于已被命令禁用的按钮

我假设IsEnabled状态是由canexecutechanged事件触发的属性,但似乎不是


哪个按钮属性受到影响?我可以挂接到该事件中,以便为按钮提供样式吗?

在viewmodel中,您可以添加一个属性,该属性将导致启用或禁用按钮。下面是一个例子

public Command FacebookLoginCommand { get; set; }

private bool _IsBusy;
public override bool IsBusy
{
    get
    {
        return _IsBusy;
    }
    set
    {
        _IsBusy = value;
        OnPropertyChanged();
        FacebookLoginCommand?.ChangeCanExecute();
        GoogleLoginCommand?.ChangeCanExecute();
    }
}

public LoginViewModel(IUserDialogs dialogs) : base(dialogs)
{
    FacebookLoginCommand = new Command(async () =>
    {
        using (Dialogs.Loading("Carregando"))
        {
            IsBusy = true;
            await Task.Run(() => new FacebookLoginService(Dialogs).Logar());
            await Task.Run(() => Task.Delay(TimeSpan.FromSeconds(3)));
            IsBusy = false;
        }
    }, CanExecute());

    private Func<bool> CanExecute()
    {
        return new Func<bool>(() => !IsBusy);
    }
}
public命令FacebookLoginCommand{get;set;}
私人厕所很忙;
公共覆盖布尔忙
{
得到
{
返回-您很忙;
}
设置
{
_IsBusy=值;
OnPropertyChanged();
FacebookLoginCommand?.ChangeCanExecute();
GoogleLoginCommand?.ChangeCanExecute();
}
}
公共登录视图模型(IUserDialogs对话框):基本(对话框)
{
FacebookLoginCommand=新命令(异步()=>
{
使用(Dialogs.load(“Carregando”))
{
IsBusy=true;
wait Task.Run(()=>newfacebook登录服务(Dialogs.Logar());
等待Task.Run(()=>Task.Delay(TimeSpan.FromSeconds(3));
IsBusy=false;
}
},CanExecute());
私有函数CanExecute()
{
返回新函数(()=>!IsBusy);
}
}

以下是用户名长度为11,密码至少为1时登录的示例

public class MainViewModel : BaseViewModel
{
    public Command LoginIn { get; set; }

    public MainViewModel()
    {
        LoginIn = new Command(async () => await SignIn(), (() => CanExecuteLogin));
    }
    private string _password;
    private string _username;
    public string UserName
    {
        get => _username;
        set
        {
            SetProperty(ref _username, value, nameof(UserName));
            SetProperty(ref _canExecuteLogin, IsExecutable(), nameof(CanExecuteLogin));
            LoginIn?.ChangeCanExecute();

        } 
    }

    public string Password
    {
        get => _password;
        set
        {
            SetProperty(ref _password, value, nameof(Password));
            SetProperty(ref _canExecuteLogin, IsExecutable(), nameof(CanExecuteLogin));
            LoginIn?.ChangeCanExecute();

        }
    }

    private bool _canExecuteLogin;

    public bool CanExecuteLogin
    {
        get => _canExecuteLogin;
        set => SetProperty(ref _canExecuteLogin, value, nameof(CanExecuteLogin));
    }

    public bool IsExecutable()
    {
        if (UserName != null && _password != null)
        {
            if (UserName.Length == 11 && _password.Length > 0)
                return true;
        }

            return false;
    }

    private async Task SignIn()
    { //Login Code here }
}