I have this attached behavior:
public enum TextType { Email, Phone, }
public static class Validator
{
public static readonly BindableProperty TextTypeProperty = BindableProperty.CreateAttached(
"TextType", typeof(TextType), typeof(Validator), null, propertyChanged: ValidateText);
public static TextType GetTextType(BindableObject view)
{
return (TextType)view.GetValue(TextTypeProperty);
}
public static void SetTextType(BindableObject view, TextType textType)
{
view.SetValue(TextTypeProperty, textType);
}
private static void TextTypeChanged(BindableObject bindable, object oldValue, object newValue)
{
var entry = bindable as Entry;
entry.TextChanged += Entry_TextChanged;
}
private static void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
var entry = sender as Entry;
bool isValid = false;
switch (GetTextType(sender as Entry))
{
case TextType.Email:
isValid = e.NewTextValue.Contains("@);
break;
case TextType.Phone:
isValid = Regex.IsMatch(e.NewTextValue, @^\d+$);
break;
default:
break;
}
if (isValid)
entry.TextColor = Color.Default;
else
entry.TextColor = Color.Red;
}
}
in XAML:
when I run the application on this page, the `TextTypeChanged` is not called, unless I change the `defaultValue` to a member of the `TextType` enum.
public enum TextType { Email, Phone, }
public static class Validator
{
public static readonly BindableProperty TextTypeProperty = BindableProperty.CreateAttached(
"TextType", typeof(TextType), typeof(Validator), null, propertyChanged: ValidateText);
public static TextType GetTextType(BindableObject view)
{
return (TextType)view.GetValue(TextTypeProperty);
}
public static void SetTextType(BindableObject view, TextType textType)
{
view.SetValue(TextTypeProperty, textType);
}
private static void TextTypeChanged(BindableObject bindable, object oldValue, object newValue)
{
var entry = bindable as Entry;
entry.TextChanged += Entry_TextChanged;
}
private static void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
var entry = sender as Entry;
bool isValid = false;
switch (GetTextType(sender as Entry))
{
case TextType.Email:
isValid = e.NewTextValue.Contains("@);
break;
case TextType.Phone:
isValid = Regex.IsMatch(e.NewTextValue, @^\d+$);
break;
default:
break;
}
if (isValid)
entry.TextColor = Color.Default;
else
entry.TextColor = Color.Red;
}
}
in XAML:
when I run the application on this page, the `TextTypeChanged` is not called, unless I change the `defaultValue` to a member of the `TextType` enum.