UITextField TextColor property binding issues (MVVMCross)

I am trying to bind the TextColor property on a UITextField in an iOS application. I am eventually trying to get the text color to change via a converter and a boolean on the viewmodel but for now I can't even get a simple binding to work.

var set = this.CreateBindingSet<MyView,MyViewModel>();
set.Bind(MyField).For(v => v.BackgroundColor).To(v => v.ExplicitUIColor); // WORKS
set.Bind(MyField).For(v => v.TextColor).To(v => v.ExplicitUIColor); // FAILS !!!
set.Apply();

MyField is defined as a UITextField and the ExplicitUIColor property is defined as returning a UIColor.

I get the error

MvxBind: Warning: 27.11 Failed to create target binding for binding TextColor for ExplicitUIColor

Any ideas please?

Thanks

Paul

Posts

  • MatthieuMatthieu USMember ✭✭

    Is it on a device? If so, it certainly is a problem with the linker removing the TextColor property http://stackoverflow.com/questions/19102503/mvvmcross-failed-to-create-target-binding-for-editingdidbegin-on-iphone

    Btw to avoid referencing an iOS specific type (UIColor) in your ViewModel , Mvvmcross provides a abstract type (MvxColor) and a built-in converter.

    See here
    https://github.com/MvvmCross/MvvmCross/wiki/Value-Converters#the-mvx-color-valueconverters

  • PaulFarrowPaulFarrow USMember

    Hey Matthieu

    That was exactly it. It had been optimized out by the linker. I added a usage to the LinkerIncludePlease.cs file and it started working.

    Also thanks for the MvxColor, I had made the code example super simple stupid and hadn't included that although that is what I am using. The converter I have, changes the text color nicely now.

    Thanks again - you're the man.

    Paul

  • This was great and very helpful. I'm doing something similar here and it works great for the most part but in some areas I'm still getting binding issues. Here is my code:

    set.Bind (currencyText).For (x => x.TextColor).To (vm => vm.AnswerTextColor).WithConversion ("NativeColor");

    In debug mode on any device this works fine. In debug mode on simulator I get:
    MvxBind:Warning: 66.81 Failed to create target binding for binding TextColor for AnswerTextColor

    In release mode it doesn't appear to be working so my assumption is I'm getting the same error/warning there too. Any ideas? Also I tried changing my Linker Behavior from "Link SDK assemblies only" to "Link all assemblies" and that didn't help any in release mode either.

  • PaulFarrowPaulFarrow USMember
    edited November 2015

    Hey Ross. Did you do what I did above you?

    I had to add it in the linkerincludeplease class like so...

    // things in this class are only required in order to prevent the linker overoptimising!
    public class LinkerIncludePlease
    {
        public void Include(UITextField textField)
        {
            textField.TextColor = textField.TextColor;
        }
    }
    

    Then all was well.

  • Boom!!!! That was it. I just copied in the code from the other URL. Seem obvious now.... Thanks for the quick reply. You are awesome

  • JamesGreen.8031JamesGreen.8031 GBMember ✭✭

    I've just been trying to do this and I get the binding blowing up with an ArgumentNullException I think on the UITextField I'm binding to. I've done all the linker tricks but still seeing this error.

Sign In or Register to comment.