MVVMCross for Touch支持双向绑定吗? (除了UITextFields) [英] Is two-way binding supported on MVVMCross for Touch? (besides UITextFields)

查看:82
本文介绍了MVVMCross for Touch支持双向绑定吗? (除了UITextFields)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找为什么我的双向绑定无法使用MVVMCross进行iOS开发的原因.我正在将UITextViews嵌入到tableView的自定义单元格中.

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.

我已经看了这个问题:

如何使用mvvmcross双向绑定到UITextView控件?

,并且提到了在Vnext中不支持与UITextViews进行2向绑定的方法,但是在V3(Hot Tuna)的beta版本中.我正在使用热金枪鱼,并获得了大约的二进制文件. 6月12日.

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 []是自定义类单元格的数组

_cells[] is an array of custom class cells

这是我的自定义单元格类中FieldDescription的属性:(FieldDescriptionLabel是我的UITextView)

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;
        }
    }

我的UITextView绑定一种方式;我确实看到了从viewModel中填充的信息,但是当我在UITextView中进行某些更改时,ViewModel不会反映这些更改.

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.

因此,主要问题是:MVVMCross Hot Tuna中的UITextView双向绑定是否起作用?如果是,对我在执行过程中做错的事情有什么想法?

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?

赞赏!

推荐答案

为使双向绑定有效,mvvmcross需要知道ui值何时更改.

In order for two-way binding to work, mvvmcross needs to know when the ui value changes.

有两种方法可以做到这一点.

There are a couple of ways to do this.

在当前设置下,也许最简单的方法是在单元格中添加public event EventHandler FieldDescriptionChanged并确保每次文本视图更改时都触发此事件-例如像这样的代码.

Perhaps the easiest way, given your current setup, is to add a public event EventHandler FieldDescriptionChanged to your cell and to make sure this event is fired every time the text view changes - e.g. with code like.

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);
    };
}


或者,您可以尝试将单元格基于具有固有DataContextMvx表格视图单元格.如果这样做,则可以直接与UITextView绑定,然后在单元格的上下文中使用数据绑定.


Alternatively, you could try basing your cell on the Mvx table view cells which have an inherent DataContext. If you do this, then you can bind directly to the UITextView with and then using data-binding within the cell's context.

N + 1个教程中显示了这种方法-例如,N = 6.5-

This approach is shown in the N+1 tutorials - for example in N=6.5 - http://slodge.blogspot.co.uk/2013/05/n6-books-over-network-n1-days-of.html - where the cell ends up with a constructor like:

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();
    });
}

使用这种方法,您只需要绑定单元的数据上下文-例如像这样:

Using this approach, you would simply need to bind the cell's data context - e.g. something like:

   this.CreateBinding (_cells[2]).For (cll => cll.DataContext).To ((TimesheetViewModel vm) => vm).TwoWay().Apply ();

这篇关于MVVMCross for Touch支持双向绑定吗? (除了UITextFields)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆