创建具有复杂对象绑定的xamarin视图

时间:2019-06-02 18:37:14

标签: validation xamarin.forms binding

我使用XamarinForms和插件ValidationRules。

我有一个帐户模型,一个登录页面链接到accountviewmodel。在我的登录页面中,添加一个xamarin视图FormEntry,它具有可绑定属性Validatable<string>类型的Validation。

bindable属性具有一些我想使用的属性:Value,Error和hasError属性。我将绑定放入我的LoginPage中,然后在FormEntry内容视图中,绑定为null。如果我得到验证对象的值,则在我的伪造textchanged事件中该值为null。

帐户模型

 public ValidatableObject<string> Password { get; set; }
 public ValidatableObject<string> Email { get; set; }
public Account()
{
    Password = new ValidatableObject<string>();
    Email = new ValidatableObject<string>();
}

private void AddValidations()
{
    // Email validations
    Email.Validations.Add(new IsNotNullOrEmptyRule<string> { ValidationMessage = C.T("Email is required.") });
    Email.Validations.Add(new EmailRule<string> { ValidationMessage = C.T("Email is not valid.") });

    //Password validations
    Password.Validations.Add(new IsNotNullOrEmptyRule<string> { ValidationMessage = C.T("A password is required.") });
    Password.Validations.Add(new LengthMinRule<string> { ValidationMessage = C.T("A password is required."), MinLength = 6 });
}

登录页面

<local:FormEntry Validation="{Binding AccountInfo.Email}" Grid.Row="2" />

FormEntry Xaml

<ContentView.Resources>

 <converter:ErrorValidationColorConverter x:Key="errorValidationColorConverter" />
  <ResourceDictionary.MergedDictionaries>

   </ResourceDictionary.MergedDictionaries>

    </ContentView.Resources>

    <ContentView.Content>
<!--<border:SfBorder  Grid.Row="2"
        Style="{DynamicResource SfBorderStyle}"
        BorderColor="{Binding Source={x:Reference emailEntry}, Path=IsFocused, Converter={StaticResource errorValidationColorConverter}, ConverterParameter={x:Reference emailEntry}}">-->

<inputLayout:SfTextInputLayout x:Name="Input"  LeadingViewPosition="Outside" TrailingViewPosition="Outside" ContainerType="None">
    <Entry TextChanged="Entry_TextChanged" x:Name="entry"

                Placeholder="Email">
    </Entry>
    <inputLayout:SfTextInputLayout.LeadingView>
        <Label
   Text="&#xf007;"  FontFamily="{StaticResource FontAwesomeSolid}">
        </Label>
    </inputLayout:SfTextInputLayout.LeadingView>
    <inputLayout:SfTextInputLayout.TrailingView>
        <Label
  x:Name="LabelError"
                Text="&#xf06a;"
                FontFamily="{StaticResource FontAwesomeSolid}"
                TextColor="{DynamicResource ErrorColor}"
                FontSize="22">
        </Label>
    </inputLayout:SfTextInputLayout.TrailingView>
</inputLayout:SfTextInputLayout>
   <!--</border:SfBorder>-->
 </ContentView.Content>

我的FormEntry CS

public FormEntry()
 {
     InitializeComponent();
  }

public ICommand ValidateUserNameCommand => new Command(() => 
ValidateField());

private bool ValidateField()
{
    return Validation.Validate();
}

public static readonly BindableProperty ValidationProperty = 
 BindableProperty.Create(
 propertyName: "Validation",
 returnType: typeof(ValidatableObject<string>),
  declaringType: typeof(FormEntry),
  defaultValue: default(ValidatableObject<string>));
  public ValidatableObject Validation
  {
get
{
  return (ValidatableObject)GetValue(ValidationProperty);
}
set
{
  SetValue(ValidationProperty, value);
}
}

protected override void OnPropertyChanged(string propertyName = null)
{
    base.OnPropertyChanged(propertyName);
    if (propertyName == ValidationProperty.PropertyName)
    {
        Input.ErrorText = Validation.Error;
        Input.HasError = Validation.Validate();
        entry.Text = Validation.Value;
        LabelError.IsVisible = Input.HasError;
    }
}

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
    ValidateField();
}
}

0 个答案:

没有答案