0

I a trying to disable and enable a button based on user input. I implemented fody property changed Nuget package, to help me reduce my code a bit.

and it works, when I start typing, the breakpoint in my LoginViewModel gets hit and display my values ViewModel getting hit every time I type

but I can't seem to trigger CanLogin() method

   [AddINotifyPropertyChangedInterface]
    public class LoginPageViewModel {

        public ICommand OpenRegisterPopupCommand { get; set; }

        public ICommand Register { get; set; }

        public ICommand Login { get; set; }

        public Users Users { get; set; }

        public bool IsPopUpOpen { get; set; }

        public LoginPageViewModel() {

            Users = new Users();

            Login = new Command(LoginAction, CanLogin);

            Register = new Command(RegisterAction);

            OpenRegisterPopupCommand = new Command(() => {
                IsPopUpOpen = true;
            });
        }

        private void LoginAction(object obj) {
            throw new NotImplementedException();
        }

        private bool CanLogin(object arg) {
            if (Users != null && !string.IsNullOrEmpty(Users.Email) && !string.IsNullOrEmpty(Users.Password)) {

                return true;
            }

            return false;
        }
edu
  • 87
  • 2
  • 8
  • Recheck the if statement of CanLogin. I make a simple code to check. The CanLogin should return true. Have you check the command. Does it been invoked correctly? – Wendy Zang - MSFT Oct 07 '21 at 09:15

1 Answers1

1

You are using a Command with a CanExecute method. If the CanExecute method returns false, the command will not ve able to be executed. But this validation does not happen all the time, you have to trigger it.

You have to call Login.ChangeCanExecute() when you modify any of the related properties (like Users, Users.Email or Users.Password), this will fire the CanExecute validation of the command.

Command documentation.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Juan Sturla
  • 1,254
  • 1
  • 4
  • 18
  • I was reading the docs but I do not get it, do you mind providing samples. I tried to modify my CanLogin() ` private bool CanLogin(object arg) { if (Users != null && !string.IsNullOrEmpty(Users.Email) && !string.IsNullOrEmpty(Users.Password)) { (Login as Command).ChangeCanExecute(); return true; } return false;` please – edu Oct 06 '21 at 14:51
  • I am not familiar with Fody, but normally you would implement `INotifyPropertyChanged` to refresh your UI. Usually when you call the `OnPropertyChanged` method asociated, you would call the `Login.ChangeCanExecute()` – Juan Sturla Oct 06 '21 at 14:55
  • According to [this](https://github.com/Fody/PropertyChanged)... Shouldn't you implement the `INotifyPropertyChanged` interface anyways? – Juan Sturla Oct 06 '21 at 14:58
  • [Here](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm#commanding-with-viewmodels) are some examples that explain a little better when to call `ChangeCanExecute`. These examples do not use Fody, but maybe you can get the idea – Juan Sturla Oct 06 '21 at 15:05
  • Then I guess it is added when being compiled. I don't know if you can suscribe to any fody OnPropertyChanged event. In case you cannot, then you should look for a workaround. Maybe making the validation inside the command (but the button will always show as enabled) – Juan Sturla Oct 06 '21 at 15:15
  • What I know is that everytime I type I can get the values email and passwor from the users – edu Oct 06 '21 at 15:40