E 'possibile associare la casella di controllo di Android per eseguire un comando sul cambiamento? Impossibile trovare un esempioMvvmCross Checkbox bind to command android xml
risposta
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;
});
}
}
- 1. MvvmCross Bind to UIButton.TitleLabel.Text
- 2. bind date to command object in Grails
- 3. MvvMCross comando di bind con parametro (nel codice C#)
- 4. Android, listener di checkbox in XML?
- 5. MVVMCross Bindings in Android
- 6. Bind Collection to StackPanel
- 7. Bind Service to BroadcastReceiver
- 8. Bind dictionary to repeater
- 9. checkbox Android stato_attivato
- 10. Bind to pgcrypto da python
- 11. std :: bind to std :: function?
- 12. ListBox TwoWay bind to SelectedItem
- 13. come aggiungere View to XML layout android
- 14. AlertDialog con checkbox In android
- 15. MVVM binding command to contextmenu item
- 16. Vim: pipe register to external command
- 17. java.lang.NoClassDefFoundError: javax/xml/bind/JAXBContext
- 18. MvvmCross MvxHttpImageView error
- 19. Android - Command not found
- 20. Winform Bind Enum to Radio Buttons
- 21. WPF Bind Window Title to ViewModel Property
- 22. Bind Xaml Radio button to boolean
- 23. Bind scroll Event To Dynamic DIV?
- 24. Ninject to bind su controller diversi
- 25. XML to C struct e C struct to XML
- 26. NSDictionary to XML
- 27. Convert to XML Dataset
- 28. selezionare XML nodo utilizzando LINQ to XML
- 29. SqliteException occupato iOS/Android Xamarin MVVMCross
- 30. Utilizzo di notifiche su Android con MvvmCross