MvvmCross UITextField custom binding

前端 未结 1 1827
广开言路
广开言路 2021-01-20 21:21

So I am trying to implement a custom binding for a UITextField in MvvmCross, pretty much along the lines of Binding \'GO\' key on Software Keyboard - i.e. trying to bind a t

相关标签:
1条回答
  • 2021-01-20 21:26

    1. What is special about PropertyInfoTargetBinding?

    The question you reference - MVVMCross Binding decimal to UITextField removes decimal point - gives the key to the difference between MvxTargetBinding and MvxPropertyInfoTargetBinding:

    • TargetBinding can be used for any arbitrary binding - e.g. for a non-propertyInfo-based binding
    • PropertyInfoTargetBinding inherits from TargetBinding and can only be used with actual C# Properties - because it uses PropertyInfo via Reflection.

    In your case, since you aren't actually using the Text property via Reflection, then I'd be tempted not to use a PropertyInfoTargetBinding and to steer clear of the Text name as well - instead just write a custom TargetBinding.

    the former is used when replacing an existing binding

    This is definitely not true - instead any binding can be used to replace another binding - as the answer on the other question says

    MvvmCross operates a simple 'last registered wins' system

    For more on custom bindings, take a look at:

    • the N=28 video in http://mvvmcross.blogspot.co.uk/
    • take a look through some of the "standard" bindings that ship with MvvmCross
      • Droid - https://github.com/MvvmCross/MvvmCross/tree/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Droid/Target
      • iOS - https://github.com/MvvmCross/MvvmCross/tree/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Target
      • note that most of these use either MvxPropertyInfoTargetBinding or MvxConvertingTargetBinding as a base class

    2. Why is my SetValue getting null?

    Your current binding code is asking for an ICommand:

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

    But your View code is currently binding the View to a string:

    // View
    set.Bind(usernameTextField).To(vm => vm.Username);
    
    // ViewModel
    private string _username;
    public string Username
    { 
        get { return _username; }
        set { _username = value; RaisePropertyChanged(() => Username); }
    }
    

    To solve this...

    1. Work out what you want to bind to - is it an ICommand (e.g. and MvxCommand) or is it a string?
    2. Change the View and the Binding to reflect this.
    0 讨论(0)
提交回复
热议问题