Search code examples
androidxamarinmvvmcross

Mvvmcross Bind Click triggered only after focus


    <CC.CustomEditText
        android:id="@+id/receptionIdentityArticle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:theme="@style/ExtendProTheme"
        android:maxLength="20"
        style="@style/ExtendProTheme.EditText"
        android:layout_below="@+id/suppliersSearchInputLabel"
        local:MvxBind=" Text ArticleSearchClause, Mode=TwoWay; EnterCommand SearchArticlesCommand; Error Errors['ArticleSearchClause']; Click OnSearchClickCommand" />

So i have a CustomEdit that is different from EditText by overriding two events

    this.KeyPress += OnEnterKeyPressed;
    this.FocusChange += OnFocusChange;

My problem is that Click command is triggered only second time i click on the EditText. First time it just gets focused then then i click it the second time the Click command triggers. I guess it's how it should work, but i would like to catch the first click it's done on the EditText. An other event maybe it is triggered but I could not find a documentation with all the possible binding on EditText. Any ideas how can i catch the first click on an EditText?


Solution

  • As @hankide said, use the Touch event instead. You will need to create a custom binding. I happen to have just dealt with this so here it is:

    public class MvxViewTouchBinding
        : MvxAndroidTargetBinding
    {
        private readonly View _view;
        private IMvxCommand _command;
    
        public MvxViewTouchBinding(View view) : base(view)
        {
            _view = view;
            _view.Touch += ViewOnTouch;
        }
    
        private void ViewOnTouch(object sender, View.TouchEventArgs eventArgs)
        {
            eventArgs.Handled = false;
    
            if (_command != null)
            {
                _command.Execute();
            }
        }
    
        public override void SetValue(object value)
        {
            _command = (IMvxCommand)value;
        }
    
        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                _view.Touch -= ViewOnTouch;
            }
            base.Dispose(isDisposing);
        }
    
        protected override void SetValueImpl(object target, object value)
        {
        }
    
        public override Type TargetType
        {
            get { return typeof(IMvxCommand); }
        }
    
        public override MvxBindingMode DefaultMode
        {
            get { return MvxBindingMode.OneWay; }
        }
    }
    

    and in your Setup.cs put

        protected override void FillTargetFactories(MvvmCross.Binding.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
        {
            base.FillTargetFactories(registry);
            registry.RegisterCustomBindingFactory<View>("Touch",
                                                      view => new MvxViewTouchBinding(view));
        }
    

    Then you can bind to Touch instead of Click.