IOS MVVMCross UITextField

LeelaPrasannaKumarLeelaPrasannaKumar USUniversity
edited October 2013 in Cross Platform with Xamarin

I was trying to bind the viewmodel with simple UITextField. I am using MVVMCross cross core only

When UITextFiled.Text content changes, my viewmodel is not updated ? I tried TwoWay binding didn't work either.

var set = this.CreateBindingSet<myIOScontroller, myviewmodel>(); set.Bind(txtresultfield).To(vm => vm.finalresult).TwoWay(); set.Apply()

Here txtresultfield is my UITextField..
and finalresult is the property in my viewmodel

After some computation in the UI I am doing it like this, txtresultfiled.Text = <somefinalvalue>

Posts

  • GuillermoGutierrezGuillermoGutierrez ✭✭✭ ESMember ✭✭✭

    That code should work, so the problem may be elsewhere.

    TwoWay is the default binding, it shouldn't be needed. Have you tried first sample of the N+1 series? It's the simplest example I could think of and has two-way binding for UITextFields.

  • GuillermoGutierrezGuillermoGutierrez ✭✭✭ ESMember ✭✭✭

    Sorry, just noticed the last line where you said:

    After some computation in the UI I am doing it like this, txtresultfiled.Text = <somefinalvalue>

    That's not correct, and maybe that's why it's not working. The ViewModel is the one that should do "some computation" and then update the ViewModel finalresult property, and not the other way around. Setting the UITextField Text property won't trigger the ValueChanged event that MvvmCross is listening to update the ViewModel property.

    So, to sum up, or you update the ViewModel property directly (recommended) or you fire the ValueChanged event manually.

  • LeelaPrasannaKumarLeelaPrasannaKumar USUniversity
    edited October 2013

    Hi thanks for the info.. I thought two-way bindings means the any updates to the UI elements will be reflected back in the ViewModel. That is how it is

    here if I have UITextField ... and if I start typing the text field.. ViewModel property value changes... (VS) if I update the text value in the code UITextField.Text = somevalue; doesn't work

    Why shouldn't this work ... and why only when user types in some text it works

  • LeelaPrasannaKumarLeelaPrasannaKumar USUniversity
    edited October 2013

    _The ViewModel is the one that should do "some computation" and then update the ViewModel finalresult property, and not the other way around.
    _
    Hey, for this .. the result is simple string. I am just setting it to Textfield.Text that's all. I have a need to do this only on the UI side and not in viewmodel, but at the same time the Viewmodel should store the selection

    _ Setting the UITextField Text property won't trigger the ValueChanged event that MvvmCross is listening to update the ViewModel property.So, to sum up, or you update the ViewModel property directly (recommended) or you fire the ValueChanged event manually._

    You have an example of this .. Thanks in advance

  • GuillermoGutierrezGuillermoGutierrez ✭✭✭ ESMember ✭✭✭

    To make it simpler, imagine you have this method:

    void DoImportantStuff() {
        // Do stuff
        string calculatedString = "calculatedStringAfterComplexOperations";
    
        txtresultfiled.Text = calculatedString;
    }
    

    Simply change it to:

    void DoImportantStuff() {
        // Do stuff
        string calculatedString = "calculatedStringAfterComplexOperations";
    
        ((YourViewModelType)ViewModel).finalresult = calculatedString;
    }
    

    The UITextField text will be updated automatically. I usually re-declare the ViewModel property to avoid the casting boilerplate in the method:

    public new EvaluateSubjectViewModel ViewModel { get { return (EvaluateSubjectViewModel)base.ViewModel; } }
    
    void DoImportantStuff() {
        // Do stuff
        string calculatedString = "calculatedStringAfterComplexOperations";
    
        ViewModel.finalresult = calculatedString;
    }
    

    Again, I'd recommend to move any operation to the ViewModel to make the code more portable. In my projects, my platform-dependent code only layouts the views and present the information, any other operation including i18n and even value formatting is performed in the cross-platform code. That makes Android development like 1/8th of the time spent developing CrossPlatform + iOS and less error prone.

  • CheesebaronCheesebaron mod DKInsider, University mod

    DoImportantStuff would also be better suited to go in a Service, which the ViewModel then uses and calls. Keep your UI code behind as clean as possible.

  • LeelaPrasannaKumarLeelaPrasannaKumar USUniversity
    edited October 2013

    Thanks for all your inputs.Will get my UI code cleaned up

Sign In or Register to comment.