Created
February 14, 2015 15:16
-
-
Save ozzieperez/cc72a3088dfca76e74d7 to your computer and use it in GitHub Desktop.
Xamarin.Forms Behavior Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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