Issue
I have below MVVM cross command in my viewmodel. I want to call this based on condition from iOS. Is this possible?
Command
public IMvxCommand LoginCommand
{
get
{
return _loginCommand ?? (_loginCommand = new MvxCommand(async () => await ExecLoginClick()));
}
}
iOS Binding
var bindings = this.CreateBindingSet<LoginView, LoginViewModel>();
bindings.Bind(username).To(vm => vm.Email);
bindings.Bind(password).To(vm => vm.Password);
bindings.Bind(login_button).To(vm => vm.LoginCommand);
bindings.Bind(forgot_button).To(vm => vm.ForgotCommand);
bindings.Bind(register_button).To(vm => vm.GetSignUpCommand);
//bindings.Bind(btn_facebook).To(vm=>vm.)
bindings.Apply();
Solution
You can use CanExecute for this.
public IMvxCommand LoginCommand
{
get
{
return _loginCommand ??
(_loginCommand = new MvxAsyncCommand(ExecLoginClick, CanLogin));
}
}
private bool CanLogin()
{
if ( /*your condition*/)
{
return true;
}
return false;
}
private Task ExecLoginClick()
{
//...
}
And in every method, that affects your condition. You have to call
LoginCommand.RaiseCanExecuteChanged();
The Button is disabled or enabled based on the return value of CanExecute.
If you want to execute your command from your view, you should inherit from the generic MvxViewController<T>
or MvxActivity<T>
like.
public class LoginView : MvxViewController<LoginViewViewModel>
// or
public class LoginView : MvxActivity<LoginViewViewModel>
And then you can call
if(/*condition*/)
{
ViewModel.LoginCommand.Execute();
}
Answered By - Sven-Michael Stübe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.