我正在研究为什么我的双向绑定没有使用MVVMCross进行iOS开发。我正在使用UITextViews嵌入tableView的自定义单元格中。
我看过这个问题:
How do I do two-way binding to a UITextView control using mvvmcross?
并且提到了如何在Vnext中支持与UITextViews的双向绑定,但它与V3(热金枪鱼)处于测试阶段。我正在使用热金枪鱼,并获得了大约二进制文件。 6月12日。
以下代码行是我如何绑定我的单元格。
this.CreateBinding (_cells[2]).For (cll => cll.FieldDescription).To ((TimesheetViewModel vm) => vm.AccountID).Mode(MvxBindingMode.TwoWay).Apply ();
_cells []是一个自定义类单元格数组
以下是我自定义单元类中FieldDescription的属性:(FieldDescriptionLabel是我的UITextView)
public string FieldDescription
{
get
{
return FieldDescriptionLabel.Text;
}
set
{
FieldDescriptionLabel.Text = value;
}
}
我的UITextView以一种方式绑定;我确实看到了从我的viewModel填充的信息,但是当我在UITextView中更改某些内容时,ViewModel并未反映这些更改。
所以主要问题:MVVMCross Hot Tuna中的UITextView双向绑定是否正常?如果是的话,对我在实施中做错了什么的想法?
欣赏它!
答案 0 :(得分:4)
为了使双向绑定有效,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
的{{1}}表格视图单元格上。如果你这样做,那么你可以直接绑定到DataContext
,然后在单元格的上下文中使用数据绑定。
这种方法在N + 1教程中显示 - 例如在N = 6.5 - http://slodge.blogspot.co.uk/2013/05/n6-books-over-network-n1-days-of.html中 - 其中单元格最终得到如下构造函数:
UITextView
使用这种方法,您只需要绑定单元格的数据上下文 - 例如类似的东西:
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();
});
}