MVVMCross十进制绑定到UITextField删除小数点 [英] MVVMCross Binding decimal to UITextField removes decimal point

查看:75
本文介绍了MVVMCross十进制绑定到UITextField删除小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将小数绑定到UITextField不允许您放置."输入"1."时,按source即可将其剥离.我知道为什么会发生这种情况,MakeSafeValue会将其转换为十进制数,而无需输入."就可以将其读回,从而覆盖输入的Text.这似乎是一个keyup vs onblur问题,很多绑定框架都可以让您覆盖它.

Binding a decimal to a UITextField does not let you put a "." in, as you type "1.", the push to source strips it out. I get why it happens, MakeSafeValue converts it to a decimal, which gets read back out without the ".", overwriting the Text entered. This seems like a keyup vs onblur issue, which a lot of binding frameworks let you override.

我知道我可以在视图模型上绑定一个字符串,但是处理EditingDidEnd而不是EditingChanged似乎更好.这样,我可以拦截ShouldEndEditing来检查有效性.

I know I could bind a string on the view model instead, but handling the EditingDidEnd instead of EditingChanged seems better. That way I could intercept ShouldEndEditing to check validity.

我将在哪里注册绑定以覆盖MvvmCross实现?我尝试添加Setup.FillTargetFactories,但它在MVXTouchBindingBuilder.FillTargetFactories之前被调用,其中MvvmCross在其中.

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

我尝试了一个不同的属性名称,在我拥有的FillTargetFactories中:

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

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

但是当我绑定时它不起作用.

but it does not work when I bind.

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

看起来像MvxPropertyInfoTargetBindingFactory.CreateBinding如果目标不具有该属性=则不起作用.这是否使我从MvxTargetBinding开始并创建自定义绑定?我讨厌在MvxPropertyInfoTargetBinding中重新创建大多数代码.

looks like MvxPropertyInfoTargetBindingFactory.CreateBinding does not work if the target does not have that property = makes sense. Does this leave me with starting at MvxTargetBinding and creating a custom binding? I hate to recreate most of the code in MvxPropertyInfoTargetBinding.

所以,我想这个问题是...

So, I guess the question(s) are...

  • 使用我的默认UITextField绑定的语法/位置是什么?
  • 如何为具有绑定语法的文本指定其他绑定?

谢谢.

推荐答案

使用我的默认UITextField绑定的语法/位置是什么?

what is the syntax/location to override the default UITextField binding with mine?

如果只想替换现有的绑定,则可以通过RegisterPropertyInfoBindingFactory扩展方法来实现.

If you want to simply replace the existing binding, then you can do this via the RegisterPropertyInfoBindingFactory extension method.

MvvmCross运行一个简单的最后注册的获胜者"系统.

MvvmCross operates a simple 'last registered wins' system.

最简单的将您的注册放在最后的位置是将其放置在SetupInitializeLastChance的末尾:

The easiest place your registration in order that happens last is to place it at the end of InitializeLastChance in your 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 (正在进行中).

如何为具有绑定语法的文本指定其他绑定?

how can I specify a different binding for Text with the Bind Syntax?

该行:

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

不起作用,因为它尝试使用名为DidEndText的属性-该属性不存在.

doesn't work because it tries to use a property called DidEndText - which doesn't exist.

解决此问题的两种可能方法是:

Two possible ways around this are:

1个子类UITextField以提供不动产

1 Subclass UITextField to provide a real property

   public class MyTextField : UITextField
   {
        // ctors

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

UITextField子类化的示例. rel ="nofollow">第N = 33个视频

An example of UITextField subclassing is shown in the N=33 video

2或使用基于非属性信息的绑定

对于已知的属性和事件对,这非常简单:

For known properties and event pairs this is pretty simple to do:

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中使用字符串"建议外,这种双文本转换的另一种可能的解决方法可能是使用双向值转换器.如果您的用户界面正在处理资金,则可能会坚持显示所有值的小数位.

As well as your 'use a string in the ViewModel' suggestion, another possible workaround for this double-text conversion, might be to use a two-way value converter. If your UI is handling money, this could insist on displaying decimal places for all values.

这篇关于MVVMCross十进制绑定到UITextField删除小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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