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

核能气质少年 提交于 2019-12-22 18:31:29

问题


I have a simple view I created using XCode (it's a XIB). It consists of two UITextView controls and a UIButton. I've exposed the UITextView controls as Outlets and given them names. Everything looks good in my View.designer.cs file.

I'm creating my binding using this syntax:

        this.AddBindings(
            new Dictionary<object, string>()
            {
                {lastname, "{'Text':{'Path':'LastName','Mode':'TwoWay'}}"},
                {uservin, "{'Text':{'Path':'CarVIN','Mode':'TwoWay'}}"}

            });

When I move from the lastname UITextView to the uservin UITextView I expect the SETTER in my viewmodel to get called, but it doesn't. When I click the button and check the value of the two text properties on my ViewModel, they are both null.

Does anybody know what I've done wrong? I've got a quick demo due on Tuesday and have all other platforms working beautifully, but just can't seem to get past this issue.

Cheers,

/j


回答1:


I think the problem will be that you are using UITextView instances... but mvvmcross vnext only ships with 2-way binding for UITextField elements.

To add the UITextView 2-way binding to an older mvvmcross version:

  1. Add a custom target binding class - a bit like this one from v3: https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding.Touch/Target/MvxUITextViewTextTargetBinding.cs

  2. Register it during Setup.cs with code like:

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories(registry);
    
        registry.RegisterFactory(new MvxSimplePropertyInfoTargetBindingFactory(typeof(MvxUITextViewTextTargetBinding), typeof(UITextView), "Text");
    }
    

Alternatively (but maybe not before your immediate demos), you could consider updating to the v3 beta release.


Incidentally, using the 'swiss' binding syntax is generally now the normal way to do this:

    this.AddBindings(
        new Dictionary<object, string>()
        {
            {lastname, "Text LastName"},
            {uservin, "Text CarVIN"}
        });

... and TwoWay binding is the default for most non-Windows MvvmCross bindings.



来源:https://stackoverflow.com/questions/15839310/how-do-i-do-two-way-binding-to-a-uitextview-control-using-mvvmcross

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!