Skip to content

Instantly share code, notes, and snippets.

@slodge
Created October 7, 2013 12:44
Show Gist options
  • Select an option

  • Save slodge/6867280 to your computer and use it in GitHub Desktop.

Select an option

Save slodge/6867280 to your computer and use it in GitHub Desktop.
public class MvxEditTextFocusChangeTextSpecialTargetBinding
: MvxAndroidTargetBinding
{
protected EditText EditText
{
get { return (EditText)Target; }
}
private bool _subscribed;
public MvxEditTextFocusChangeTextSpecialTargetBinding(EditText view)
: base(view)
{
SubscribeToEvents();
}
protected override void SetValue(object value)
{
var editText = EditText;
if (editText == null)
return;
value = value ?? string.Empty;
editText.Text = value.ToString();
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.TwoWay; }
}
public void SubscribeToEvents()
{
var editText = EditText;
if (editText == null)
return;
editText.FocusChange += HandleFocusChange;
_subscribed = true;
}
private void HandleFocusChange(object sender, View.FocusChangeEventArgs e)
{
var editText = EditText;
if (editText == null)
return;
if (!e.HasFocus)
FireValueChanged(editText.Text);
}
public override Type TargetType
{
get { return typeof(string); }
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
var editText = EditText;
if (editText != null && _subscribed)
{
editText.FocusChange -= HandleFocusChange;
_subscribed = false;
}
}
base.Dispose(isDisposing);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment