MvvmCross Checkbox binds to android xml command

Is it possible to link android to execute a command when changing? Could not find an example.

+5
source share
1 answer

A standard approach would be to simply bind a property of type bool in your viewmodel and execute your logic in setting this property. Your binding will look like this:

local:MvxBind="Checked IsChecked" 

However, if you really need a binding to the command, you can also bind the Click event:

 local:MvxBind="Checked IsChecked; Click YourCommand;" 

ViewModel:

 private bool _isChecked; public bool IsChecked { get { return _isChecked; } set { _isChecked = value; RaisePropertyChanged(() => IsChecked); } } public ICommand YourCommand { get { return new MvxCommand(() => { var isChecked = IsChecked; //Now you can use isChecked variable }); } } 

Please note that you do not get the flag value in your command parameter, so you need to bind to the bool property anyway. Another problem with this solution is that you must rely on the fact that the installer of your property will be called before your command.

If you really need to have a command with the bool parameter, you can do this. The amazing thing about the structure of MvvmCross is that you can always expand its functionality. In your case, you will need to implement custom binding for CheckBox. A good starting point could be here: http://slodge.blogspot.cz/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html

Edit:. To show how easy I let him try and implement a simple command binding with the bool parameter. (No CanExecute validation). In case anyone is interested, here is the code.
Binding Class:

 public class CheckBoxChangedBinding : MvxAndroidTargetBinding { private ICommand _command; protected CheckBox View { get { return (CheckBox) Target; } } public CheckBoxChangedBinding(CheckBox view) : base(view) { view.CheckedChange += CheckBoxOnCheckedChange; } private void CheckBoxOnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e) { if (_command == null) return; var checkBoxValue = e.IsChecked; _command.Execute(checkBoxValue); } protected override void SetValueImpl(object target, object value) { _command = value as ICommand; } public override MvxBindingMode DefaultMode { get { return MvxBindingMode.OneWay; } } public override Type TargetType { get { return typeof (ICommand); } } protected override void Dispose(bool isDisposing) { if (isDisposing) { var view = View; if (view != null) { view.CheckedChange -= CheckBoxOnCheckedChange; } } base.Dispose(isDisposing); } } 

In Setup.cs:

 protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry) { base.FillTargetFactories(registry); registry.RegisterCustomBindingFactory<CheckBox>("CheckedChanged", checkBox => new CheckBoxChangedBinding(checkBox)); } 

In your layout:

 <CheckBox android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" local:MvxBind="CheckedChanged CheckBoxCheckedCommand" /> 

And finally, ViewModel:

 public ICommand CheckBoxCheckedCommand { get { return new MvxCommand<bool>(isChecked => { var parameter = isChecked; }); } } 
+14
source

All Articles