2

I was looking at reasons why my 2-way binding hasn't been working for iOS development using MVVMCross. I'm using UITextViews embedding in custom cells of a tableView.

I've looked at this question:

How do I do two-way binding to a UITextView control using mvvmcross?

and there was mention on how 2-way binding with UITextViews isn't supported in Vnext, but it is in beta with V3 (Hot Tuna). I am using Hot Tuna, and got the binaries approx. June 12th.

The following line of code is how I'm binding my cell.

this.CreateBinding (_cells[2]).For (cll => cll.FieldDescription).To ((TimesheetViewModel vm) => vm.AccountID).Mode(MvxBindingMode.TwoWay).Apply ();

_cells[] is an array of custom class cells

Here's the property of FieldDescription in my custom cell class: (FieldDescriptionLabel is my UITextView)

    public string FieldDescription
    {
        get
        { 
            return FieldDescriptionLabel.Text;
        }
        set 
        {
            FieldDescriptionLabel.Text = value;
        }
    }

My UITextView binds one way; I do see the information populated from my viewModel, but when I change something in the UITextView, the ViewModel doesn't reflect those changes.

So the main question: Is 2-way binding working for UITextView in MVVMCross Hot Tuna? If yes, any ideas on what I'm doing wrong in my implementation?

Appreciate it!

4

1 回答 1

4

为了使双向绑定起作用,mvvmcross 需要知道 ui 值何时更改。

有几种方法可以做到这一点。

考虑到您当前的设置,也许最简单的方法是将 a 添加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. 如果这样做,则可以直接绑定到UITextViewwith,然后在单元格的上下文中使用数据绑定。

这种方法显示在 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 ();
于 2013-06-17T20:52:22.207 回答