我在找原因,为什么我的双向绑定不适用于使用MVVMCross的iOS开发。我在tableView的自定义单元格中使用UITextViews嵌入。
我看过这个问题:
How do I do two-way binding to a UITextView control using mvvmcross?
还有提到在Vnext中不支持与UITextViews的双向绑定,但它正在与V3 (热金枪鱼)进行测试版。我正在使用热金枪鱼,并得到了二进制文件的近似。6月12日。
下面这行代码是我绑定cell的方式。
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热金枪鱼中对UITextView有效吗?如果是,那么我在实现中做错了什么呢?
非常感谢!
发布于 2013-06-18 04:52:22
为了使双向绑定起作用,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);
};
}或者,您可以尝试将单元格建立在具有固有DataContext的Mvx表格视图单元格上。如果这样做,则可以使用直接绑定到UITextView,然后在单元格的上下文中使用数据绑定。
在N+1教程中-例如在N=6.5 - 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 ();https://stackoverflow.com/questions/17156368
复制相似问题