【发布时间】:2015-09-27 06:38:59
【问题描述】:
我是 MVVMCross 的新手。我正在通过子类化 MVXTableVIewCell 创建自定义表格视图单元格。我在单元格中有一个删除按钮。当用户单击删除按钮时,将从表中删除相同的记录。我不确定如何将单元格中的删除按钮绑定到视图模型类。下图是 ViewModel 类、Custom cell 类和 RelayCommand 类。
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);
}
}
我不知道如何将我的 DebriefViewCell 中的“deleteDebriefBttn”绑定到 DebriefViewModel 中的 DeleteDebriefCommand。请帮帮我。
【问题讨论】:
标签: xamarin xamarin.ios mvvmcross