2017-03-07 64 views
1
<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" /> 

所以我有一個CustomEdit通過重寫兩個事件Mvvmcross綁定僅單擊焦點後

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

我的問題是,單擊命令只觸發第二次是從EditText上不同的我點擊的EditText觸發。第一次它只是聚焦,然後第二次點擊命令觸發點擊它。我想這是它應該如何工作,但我想趕上第一次點擊它在EditText上完成。另一個事件可能會被觸發,但是我無法在EditText上找到所有可能綁定的文檔。任何想法如何捕獲第一次點擊EditText?

回答

1

正如@hankide所說的,改用Touch事件。您將需要創建一個自定義綁定。我恰巧剛剛處理了這所以在這裏,它是:

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; } 
    } 
} 

,並在您Setup.cs把

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

然後,你可以綁定到觸摸,而不是點擊。

+0

我會試一下。謝謝! – CiucaS

2

您可以使用Touch事件而不是Click來讓事件在第一次點擊時觸發。不幸的是,你描述的行爲與Android是正常的(即使令人困惑),並且與MVVMCross無關。