public void AddValidation(AutoFormsValidationAttribute validation, View control, string title) { if (string.IsNullOrWhiteSpace(title)) { title = "Item"; } title = ControlBase.GetLocalizedString(title); var label = new Label { IsVisible = false, IsEnabled = false, TextColor = _clrValidationText, Margin = new Thickness(3, 0, 0, 3) }; switch (validation) { case AutoFormsMaxLengthAttribute max when validation.Type == AutoFormsValidationAttribute.ValidationType.MaxLength: label.Text = string.Format(ControlBase.GetLocalizedString(AutoFormsConstants.ValidationMaxLength), title, max.Length); MonitorEntryControl(true, validation, control, label); break; case AutoFormsMinLengthAttribute min when validation.Type == AutoFormsValidationAttribute.ValidationType.MinLength: label.Text = string.Format(ControlBase.GetLocalizedString(AutoFormsConstants.ValidationMinLength), title, min.Length); break; case AutoFormsValidationAttribute _ when validation.Type == AutoFormsValidationAttribute.ValidationType.Numeric: label.Text = string.Format(ControlBase.GetLocalizedString(AutoFormsConstants.ValidationNumeric), title); break; case AutoFormsValidationAttribute _ when validation.Type == AutoFormsValidationAttribute.ValidationType.Email: label.Text = string.Format(ControlBase.GetLocalizedString(AutoFormsConstants.ValidationEmail), title); MonitorEntryControl(false, validation, control, label); break; case AutoFormsValidationAttribute _ when validation.Type == AutoFormsValidationAttribute.ValidationType.Required: label.Text = string.Format(ControlBase.GetLocalizedString(AutoFormsConstants.ValidationRequired), title); MonitorEntryControl(false, validation, control, label); break; default: label = null; break; } if (label == null) { return; } _stack.Children.Add(label); _validations.Add(new Validation(validation, control, label)); }
View CreateControlView( PropertyInfo property, AutoFormsListItemAttribute attribute, ControlList listControl) { Style style = null; if (string.IsNullOrEmpty(attribute.ItemStyle) == false && Application.Current.Resources.TryGetValue(attribute.ItemStyle, out object obj)) { style = (Style)obj; } View v = null; switch (attribute.Type) { case AutoFormsType.DateTime: v = new DatePicker { Style = style }; v.SetBinding(DatePicker.DateProperty, new Binding(property.Name, BindingMode.TwoWay, new DateTimeConverter(), property.PropertyType)); break; case AutoFormsType.Entry: var maxLength = property.GetAttribute <AutoFormsMaxLengthAttribute>()?.Length ?? 0; InputView iv; if (attribute.HeightRequest > 0) { iv = new Editor { Style = style, Placeholder = ControlBase.GetLocalizedString(attribute.Placeholder), HeightRequest = attribute.HeightRequest, }; iv.SetBinding(Editor.TextProperty, new Binding(property.Name, BindingMode.TwoWay)); } else { iv = new Entry { Style = style, Placeholder = ControlBase.GetLocalizedString(attribute.Placeholder), }; iv.SetBinding(Entry.TextProperty, new Binding(property.Name, BindingMode.TwoWay)); } if (maxLength > 0) { iv.MaxLength = maxLength; } v = iv; break; case AutoFormsType.Checkbox: //v = new Checkbox //{ // HorizontalOptions = LayoutOptions.Center, //}; //v.SetBinding(Checkbox.CheckedProperty, new Binding(property.Name, BindingMode.TwoWay)); break; case AutoFormsType.Button: var btnAttrib = property.GetAttribute <AutoFormsButtonAttribute>(); if (btnAttrib != null && string.IsNullOrEmpty(btnAttrib.ButtonStyle) == false && Application.Current.Resources.TryGetValue(btnAttrib.ButtonStyle, out object btnObj)) { style = (Style)btnObj; } v = new Button { Style = style, Text = ControlBase.GetLocalizedString(btnAttrib?.Text), HorizontalOptions = LayoutOptions.CenterAndExpand, VerticalOptions = LayoutOptions.CenterAndExpand, }; v.SetBinding(Button.CommandParameterProperty, new Binding(".")); v.SetBinding(Button.CommandProperty, new Binding(property.Name)); break; case AutoFormsType.Combo: v = new Picker(); var propertyType = property.PropertyType; var t = Nullable.GetUnderlyingType(propertyType); if (t != null && t.IsEnum) { propertyType = t; var dict = EnumHelper.ToDictionary(propertyType); var items = new List <ControlCombo.EnumItem>(); items.Add(new ControlCombo.EnumItem { Title = "Please Select", Value = -1 }); int index = 0; foreach (var d in dict) { items.Add(new ControlCombo.EnumItem { Title = d.Value, Value = index++ }); } var picker = v as Picker; picker.ItemsSource = items; picker.SetBinding( Picker.SelectedIndexProperty, new Binding(property.Name, BindingMode.TwoWay, new ControlCombo.EnumItemConverter(), propertyType)); } else if (property.PropertyType.IsEnum) { var picker = v as Picker; var dict = EnumHelper.ToDictionary(property.PropertyType); picker.ItemsSource = dict.Values.ToList(); picker.SetBinding( Picker.SelectedIndexProperty, new Binding(property.Name, BindingMode.TwoWay, new EnumConverter(), property.PropertyType)); } break; default: style = style ?? listControl?.LabelStyle ?? null; v = new Label { Style = style, VerticalOptions = LayoutOptions.CenterAndExpand, VerticalTextAlignment = TextAlignment.Center, HorizontalOptions = LayoutOptions.Fill, HorizontalTextAlignment = attribute.HorizontalItemAlignment, LineBreakMode = LineBreakMode.TailTruncation, MaxLines = 1, }; v.SetBinding(Label.TextProperty, new Binding(property.Name, converter: new DisplayConverter())); break; } return(v); }