第一个。根据您的要求,您必须在布局中添加单独的图片:
<StackLayout
Orientation="Horizontal">
<Entry Placeholder="Enter a System.Double">
<Entry.Behaviors>
<local:CustomBehavior x:Name="customValidator"/>
</Entry.Behaviors>
</Entry>
<Image
HeightRequest="24"
WidthRequest="24"
Aspect="AspectFit"
IsVisible="{Binding Source={x:Reference customValidator},
Path=IsVisible}"
Style="{Binding Source={x:Reference customValidator},
Path=IsValid,
Converter={StaticResource boolToStyleImage}}"/>
</StackLayout>
注意 x:Name="" 属性,因为这是在此 xaml 文件中引用该自定义行为所必需的
第二次。在您的“CustomBehavior”上为两个字段创建 BindableProperties,您将在这两个字段上绑定您的条目的图像状态
public class CustomBehavior: Behavior<Entry>
{
private const string digitRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";
static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly ("IsValid", typeof (bool), typeof (CustomBehavior), false);
public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;
static readonly BindablePropertyKey IsVisiblePropertyKey = BindableProperty.CreateReadOnly ("IsVisible", typeof (bool), typeof (CustomBehavior), false);
public static readonly BindableProperty IsVisibleProperty = IsVisiblePropertyKey.BindableProperty;
private const string FRIEND = "friend";
private const string FRIENDS = "friends";
public bool IsValid
{
get { return (bool)base.GetValue (IsValidProperty); }
private set { base.SetValue (IsValidPropertyKey, value); }
}
public bool IsVisible
{
get { return (bool)base.GetValue (IsVisibleProperty); }
private set { base.SetValue (IsVisiblePropertyKey, value); }
}
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 e)
{
if (e.NewTextValue.Length > 0)
{
IsVisible = true;
Entry entry =(Entry)sender;
IsValid = Regex.IsMatch(e.NewTextValue, digitRegex);
if(IsValid) // Check only if we have a valid email
{
// Here we validate if the email contains our requirements
String email = entry.Text;
int pos = email.IndexOf("@"); // Exclude the domain
string username = email.Substring(0, pos);
if(username.Contains(FRIEND) || username.Contains(FRIENDS))
{
IsValid = true;
}else
IsValid = false;
}
}
}else
IsVisible = false;
}
}
3d。创建一个简单的 ValueConverter 类,它将一个布尔值转换为我们的“T”类型的对象之一,具体取决于我们将此转换器附加到的对象。
namespace YourApp
public class BooleanToObjectConverter<T> : IValueConverter
{
public T FalseObject { set; get; }
public T TrueObject { set; get; }
public object Convert (object value, Type targetType,
object parameter, CultureInfo culture)
{
return (bool)value ? this.TrueObject : this.FalseObject;
}
public object ConvertBack (object value, Type targetType,
object parameter, CultureInfo culture)
{
return ((T)value).Equals (this.TrueObject);
}
}
4th. 将此样式添加到 App.xaml 文件的 ResourceDictionary 标记中,这将声明 TrueObject(有效电子邮件的样式)和 FalseObject(无效电子邮件的样式)。
将“your_wrong_image_here.png”和“your_correct_image_here.png”替换为您想要的图片
<Application
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourApp.App"
xmlns:statics="clr-namespace:YourApp">
<Application.Resources>
<!-- Application resource dictionary -->
<ResourceDictionary>
<statics:BooleanToObjectConverter x:Key="boolToStyleImage"
x:TypeArguments="Style">
<statics:BooleanToObjectConverter.FalseObject>
<Style TargetType="Image">
<Setter Property="HeightRequest" Value="24" />
<Setter Property="Source"
Value="your_wrong_image_here.png" />
</Style>
</statics:BooleanToObjectConverter.FalseObject>
<statics:BooleanToObjectConverter.TrueObject>
<Style TargetType="Image">
<Setter Property="HeightRequest" Value="24" />
<Setter Property="Source"
Value="your_correct_image_here.png" />
</Style>
</statics:BooleanToObjectConverter.TrueObject>
</statics:BooleanToObjectConverter>
</ResourceDictionary>
</Application.Resources>
</Application>
这应该可以满足您的需求,只是要小心复制粘贴错误,因为您的项目中有不同的类名!