Skip to content

Instantly share code, notes, and snippets.

@ozzieperez
Created February 14, 2015 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozzieperez/cc72a3088dfca76e74d7 to your computer and use it in GitHub Desktop.
Save ozzieperez/cc72a3088dfca76e74d7 to your computer and use it in GitHub Desktop.
Xamarin.Forms Behavior Example
//Add functionality to visual elements without subclassing.
//Xamarin.Form controls can have a list of Behaviors.
public class NumericValidationBehavior : Behavior<Entry>
{
protected override void OnAttachedTo(Entry entry)
{
entry.TextChanged += OnEntryTextChanged;
base.OnAttachedTo(entry);
}
protected override void OnDetachingFrom(Entry entry)
{
entry.TextChanged -= OnEntryTextChanged;
base.OnDetachingFrom(entry);
}
void OnEntryTextChanged(object sender, TextChangedEventArgs args)
{
double result;
bool isValid = Double.TryParse(args.NewTextValue, out result);
((Entry)sender).TextColor = isValid ? Color.Default : Color.Red;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment