CiucaS
CiucaS

Reputation: 2128

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?

Upvotes: 1

Views: 428

Answers (2)

Jim Wilcox
Jim Wilcox

Reputation: 1573

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.

Upvotes: 1

Timo Salom&#228;ki
Timo Salom&#228;ki

Reputation: 7189

You can use the Touch event instead of Click to get the event to fire on the first click. Unfortunately, the behavior you described is normal to Android (even though confusing) and isn't related to MVVMCross.

Upvotes: 2

Related Questions