2015-05-13 24 views

risposta

14

L'approccio standard sarebbe semplicemente collegarsi alla proprietà di tipo bool nel tuo viewmodel ed eseguire la tua logica nel setter di questa proprietà. La vostra associazione sarà quindi simile a questa:

local:MvxBind="Checked IsChecked" 

Tuttavia, se si ha realmente bisogno legarsi al comando, è anche possibile associare all'evento Click:

local:MvxBind="Checked IsChecked; Click YourCommand;" 

ViewModel:

private bool _isChecked; 

public bool IsChecked 
{ 
    get { return _isChecked; } 
    set 
    { 
     _isChecked = value; 
     RaisePropertyChanged(() => IsChecked); 
    } 
} 

public ICommand YourCommand 
{ 
    get 
    { 
     return new MvxCommand(() => 
     { 
      var isChecked = IsChecked; 
      //Now you can use isChecked variable 
     }); 
    } 
} 

Si noti che non si riceve il valore della casella di controllo nel parametro di comando, quindi è necessario associare comunque la proprietà bool. Un altro problema con questa soluzione è che devi fare affidamento su un fatto, che il setter della tua proprietà sarebbe chiamato prima del tuo comando.

Se è davvero necessario avere il comando con il parametro bool, allora si può sicuramente farlo. La cosa meravigliosa del framework MvvmCross è che puoi sempre estenderne le funzionalità. Nel tuo caso dovresti implementare l'associazione personalizzata per CheckBox. Un buon punto di partenza può essere qui: http://slodge.blogspot.cz/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html

Modifica: Per mostrare quanto sia facile ho dato una prova e implementare il semplice comando vincolante con parametro bool. (Nessun controllo CanExecute). Nel caso qualcuno sia interessato, ecco il codice.
classe Binding:

public class CheckBoxChangedBinding 
    : MvxAndroidTargetBinding 
{ 
    private ICommand _command; 

    protected CheckBox View 
    { 
     get { return (CheckBox) Target; } 
    } 

    public CheckBoxChangedBinding(CheckBox view) 
     : base(view) 
    { 
     view.CheckedChange += CheckBoxOnCheckedChange; 

    } 

    private void CheckBoxOnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e) 
    { 
     if (_command == null) 
      return; 
     var checkBoxValue = e.IsChecked; 
     _command.Execute(checkBoxValue); 
    } 


    protected override void SetValueImpl(object target, object value) 
    { 
     _command = value as ICommand; 
    } 

    public override MvxBindingMode DefaultMode 
    { 
     get { return MvxBindingMode.OneWay; } 
    } 

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

    protected override void Dispose(bool isDisposing) 
    { 
     if (isDisposing) 
     { 
      var view = View; 
      if (view != null) 
      { 
       view.CheckedChange -= CheckBoxOnCheckedChange; 
      } 
     } 
     base.Dispose(isDisposing); 
    } 
} 

In Setup.cs:

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry) 
{ 
    base.FillTargetFactories(registry); 
    registry.RegisterCustomBindingFactory<CheckBox>("CheckedChanged", 
     checkBox => new CheckBoxChangedBinding(checkBox)); 
} 

nel layout:

<CheckBox 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    local:MvxBind="CheckedChanged CheckBoxCheckedCommand" /> 

E infine ViewModel:

public ICommand CheckBoxCheckedCommand 
{ 
    get 
    { 
     return new MvxCommand<bool>(isChecked => 
     { 
      var parameter = isChecked; 
     }); 
    } 
}