为了使双向绑定工作,mvvmcross 需要知道 ui 值何时发生变化。
有几种方法可以做到这一点。
考虑到您当前的设置,也许最简单的方法是添加public event EventHandler FieldDescriptionChanged
到您的单元格并确保每次文本视图更改时都会触发此事件 - 例如与类似的代码。
public event EventHandler FieldDescriptionChanged;
public string FieldDescription
{
get
{
return FieldDescriptionLabel.Text;
}
set
{
FieldDescriptionLabel.Text = value;
}
}
public override void AwakeFromNib()
{
base.AwakeFromNib();
FieldDescriptionLabel.Changed += (s,e) => {
var handler = FieldDescriptionChanged;
if (handler != null)
handler(this, EventArgs.Empty);
};
}
或者,您可以尝试将您的单元基于Mvx
表格视图单元格具有固有的DataContext
。如果这样做,那么您可以直接绑定到UITextView
with 然后在单元格上下文中使用数据绑定。
这种方法在 N+1 教程中有所展示 - 例如 N=6.5 -http://slodge.blogspot.co.uk/2013/05/n6-books-over-network-n1-days-of.html http://slodge.blogspot.co.uk/2013/05/n6-books-over-network-n1-days-of.html- 单元格最终的构造函数如下:
public BookCell (IntPtr handle) : base (handle)
{
_loader = new MvxImageViewLoader(() => MainImage);
this.DelayBind(() => {
var set = this.CreateBindingSet<BookCell, BookSearchItem> ();
set.Bind(TitleLabel).To (item => item.volumeInfo.title);
set.Bind (AuthorLabel).To (item => item.volumeInfo.authorSummary);
set.Bind (_loader).To (item => item.volumeInfo.imageLinks.thumbnail);
set.Apply();
});
}
使用这种方法,您只需要绑定单元格的数据上下文 - 例如就像是:
this.CreateBinding (_cells[2]).For (cll => cll.DataContext).To ((TimesheetViewModel vm) => vm).TwoWay().Apply ();