I am new to MVVMCross. I am creating a custom table view cell by subclassing MVXTableVIewCell. I have a delete button in the cell. When the user clicks on the delete button the same record will be deleted from the table. I am not sure how to bind the delete button present in my cell to the view model class. Below shown is the ViewModel class,Custom cell class and RelayCommand class.
public class DebriefViewModel: MvxViewModel, INotifyPropertyChanged
{
public RelayCommand DeleteDebriefCommand { get; set; }
public DebriefViewModel()
{
DeleteDebriefCommand = new RelayCommand(DoDeleteDebrief);
}
public async void DoDeleteDebrief(object param)
{
Debrief debriefDelete = (Debrief)param;
//Code to delete the debrief.
}
}
public partial class DebriefViewCell : MvxTableViewCell
{
public static readonly UINib Nib = UINib.FromName("DebriefViewCell", NSBundle.MainBundle);
public static readonly NSString Key = new NSString ("DebriefViewCell");
public DebriefViewCell (IntPtr handle) : base(BindingText,handle)
{
this.DelayBind(() => {
var set = this.CreateBindingSet<DebriefViewCell, DebriefViewModel>();
//Not sure how to bind the deleteDebriefBttn
set.Bind(deleteDebriefBttn).To(vm => vm.DeleteDebriefCommand);
set.Apply();
});
}
public static DebriefViewCell Create ()
{
return (DebriefViewCell)Nib.Instantiate (null, null) [0];
}
}
public class RelayCommand : ICommand
{
// Event that fires when the enabled/disabled state of the cmd changes
public event EventHandler CanExecuteChanged;
// Delegate for method to call when the cmd needs to be executed
private readonly Action<object> _targetExecuteMethod;
// Delegate for method that determines if cmd is enabled/disabled
private readonly Predicate<object> _targetCanExecuteMethod;
public bool CanExecute(object parameter)
{
return _targetCanExecuteMethod == null || _targetCanExecuteMethod(parameter);
}
public void Execute(object parameter)
{
// Call the delegate if it's not null
if (_targetExecuteMethod != null) _targetExecuteMethod(parameter);
}
public RelayCommand(Action<object> executeMethod, Predicate<object> canExecuteMethod = null)
{
_targetExecuteMethod = executeMethod;
_targetCanExecuteMethod = canExecuteMethod;
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty);
}
}
I do not know how to bind “deleteDebriefBttn” present in my DebriefViewCell to the DeleteDebriefCommand present in the DebriefViewModel. Please help me.