Enlace personalizado MvvmCross UITextField
Así que estoy tratando de implementar un enlace personalizado para un UITextField en MvvmCross, más o menos en la línea deEnlace de tecla 'GO' en el teclado del software - es decir, intentar vincular un campo de texto para activar automáticamente un evento cuando se toca el botón Listo en el teclado (de modo vinculante paraShouldReturn
) También necesito vincular el campo de textoEditingDidBegin
yEditingDidEnd
eventos. Debido a que estoy vinculando más de un evento, he creado unMvxPropertyInfoTargetBinding
como sigue:
public class MyTextFieldTargetBinding : MvxPropertyInfoTargetBinding<UITextField>
{
private ICommand _command;
protected UITextField TextField
{
get { return (UITextField)Target; }
}
public MyTextFieldTargetBinding(object target, PropertyInfo targetPropertyInfo) : base(target, targetPropertyInfo)
{
TextField.ShouldReturn += HandleShouldReturn;
TextField.EditingDidBegin += HandleEditingDidBegin;
TextField.EditingDidEnd += HandleEditingDidEnd;
}
private bool HandleShouldReturn(UITextField textField)
{
if (_command == null) {
return false;
}
var text = textField.Text;
if (!_command.CanExecute (text)) {
return false;
}
textField.ResignFirstResponder();
_command.Execute(text);
return true;
}
private void HandleEditingDidBegin (object sender, EventArgs e)
{
// do something
}
private void HandleEditingDidEnd (object sender, EventArgs e)
{
// do something
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
public override void SetValue(object value)
{
var command = value as ICommand;
_command = command;
}
public override Type TargetType
{
get { return typeof(ICommand); }
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
if (TextField != null)
{
TextField.ShouldReturn -= HandleShouldReturn;
TextField.EditingDidBegin -= HandleEditingDidBegin;
TextField.EditingDidEnd -= HandleEditingDidEnd;
}
}
base.Dispose(isDisposing);
}
}
Mi primera pregunta es: ¿estoy en lo correcto al crear uno?MvxPropertyInfoTargetBinding
para todos los eventos? Relacionado, no entiendo la diferencia entreMvxPropertyInfoTargetBinding
yMvxTargetBinding
. De acuerdo aMVVMCross El enlace decimal a UITextField elimina el punto decimal el primero se usa al reemplazar un enlace existente, el segundo para propiedades conocidas y pares de eventos. Entonces, ¿estoy usando el correcto?
En segundo lugar (y el verdadero quid de mi problema), mi código funcionaexcepto por SetValue
- se dispara, pero elvalue
esnull
. Esto es lo que tengo en miSetup
archivo:
protected override void FillTargetFactories (IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories (registry);
registry.RegisterPropertyInfoBindingFactory(typeof(MyTextFieldTargetBinding), typeof(UITextField), "Text");
}
No hago nada en miView
- tal vez ahí es donde radica el problema?
EDITAR:
MiViewModel
:
public class LoginViewModel : MvxViewModel
{
private string _username;
public string Username
{
get { return _username; }
set { _username = value; RaisePropertyChanged(() => Username); }
}
private string _password;
public string Password
{
get { return _password; }
set { _password = value; RaisePropertyChanged(() => Password); }
}
private MvxCommand _login;
public ICommand Login
{
get {
_login = _login ?? new MvxCommand(DoLogin);
return _login;
}
}
public LoginViewModel(ILoginManager loginManager)
{
_loginManager = loginManager;
}
private void DoLogin()
{
// call the login web service
}
}
En mi 'Vista', no hago nada elegante (creo los elementos de Vista en una XIB):
public override void ViewDidLoad()
{
base.ViewDidLoad ();
this.NavigationController.SetNavigationBarHidden(true, false);
var set = this.CreateBindingSet<LoginView, Core.ViewModels.LoginViewModel>();
set.Bind(usernameTextField).To(vm => vm.Username);
set.Bind(passwordTextField).To(vm => vm.Password);
set.Bind (loginButton).To (vm => vm.Login);
set.Apply();
}
No hay mensajes de seguimiento interesantes.