Xamarin 表单 Commanding CanExecute 不会更新 Button IsEnabled

whi*_*der 2 xamarin xamarin.forms

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

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

什么 Button 属性受到影响,我可以连接到此事件以便我可以为按钮提供样式吗?

小智 5

在您的视图模型中,您可以添加一个属性来启用或禁用其按钮。下面是一个例子。

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);
    }
}
Run Code Online (Sandbox Code Playgroud)