MVVMCross Associating a decimal with a UITextField removes a decimal

Binding a decimal to a UITextField does not allow you to put a ".". in, when you type "1.", clicking on the source blocks it. I understand why this happens, MakeSafeValue converts it to decimal, which is read without ".", Overwriting the entered text. This seems like a problem with keyup vs onblur, with which many binding mechanisms allow you to override.

I know that I could bind a string to a view model, but processing EditingDidEnd instead of EditingChanged looks better. That way I could intercept ShouldEndEditing to validate.

Where would I register my binding to override the implementation of MvvmCross? I tried adding to my Setup.FillTargetFactories, but it is called before MVXTouchBinding.FillTargetFactories, where is MvvmCross.

I tried a different property name, in FillTargetFactories I have:

registry.RegisterPropertyInfoBindingFactory(typeof (UITextFieldTextDidEndTargetBinding), typeof (UITextField), "DidEndText");

but it does not work when I contact.

set.Bind(textField).For("DidEndText").To(vm => vm.GMoney);

looks like MvxPropertyInfoTargetBindingFactory.CreateBinding does not work if the target does not have this property = makes sense. Does this mean that I start with MvxTargetBinding and create a custom binding? I don't like to recreate most of the code in MvxPropertyInfoTargetBinding.

So, I think the question (s) ...

  • What is the syntax / location to override the default UITextField binding with mine?
  • How can I specify a different binding for text with binding syntax?

Thanks in advance.

+1
1

/ UITextField ?

, RegisterPropertyInfoBindingFactory.

MvvmCross " ".

InitializeLastChance Setup:

 protected override void InitializeLastChance()
 {
     base.InitializeLastChance();

     var factory = base.Resolve<IMvxTargetBindingFactoryRegistry>();
     factory.RegisterPropertyInfoBindingFactory(
                     typeof(MyBindingType),
                     typeof(UITextField),
                     "Text");
 }

/ https://github.com/slodge/MvvmCross/wiki/Customising-using-App-and-Setup ( ).

?

:

     registry.RegisterPropertyInfoBindingFactory(
            typeof (UITextFieldTextDidEndTargetBinding), 
            typeof (UITextField), 
            "DidEndText");

, DidEndText - .

:

1 UITextField,

   public class MyTextField : UITextField
   {
        // ctors

        public string MySpecialText
        {
            get { return base.Text; }
            set { base.Text = value; }
        }
   }

UITextField N = 33

2 -propertyInfo

:

public class MySpecialBinding : MvxTargetBinding
{
    public MySpecialBinding (UIEditField edit)
        : base(edit)
    {
        edit.SpecialEvent += Handler;
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            var text = (UITextField)Target;
            if (text != null)
            {
                text.SpecialEvent -= Handler;
            }
        }
        base.Dispose(isDisposing);
    }

    protected override void Handler(object s, EventArgs e)
    {
        var text = (UITextField)target;
        if (text == null)
            return;
        FireValueChanged(text.Text);
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.TwoWay; }
    }

    public override Type TargetType
    {
        get { return typeof(string); }
    }

    public override void SetValue(object value)
    {
        var text = (UITextField)target;
        if (text == null)
            return;
        text.Text = value;
    }
}

RegisterCustomBindingFactory

registry.RegisterCustomBindingFactory<UITextField>(
        "FooFlipper", 
        textField => new MySpecialBinding(textField));

:

set.Bind(textField).For("FooFlipper").To(vm => vm.GMoney);

.


" ViewModel", . , .

+4

All Articles