Пример #1
0
        protected override View CreateControl(string bindingName, Type fieldType)
        {
            if (fieldType != typeof(bool) && fieldType != typeof(bool?))
            {
                Debug.WriteLine($"field:{bindingName} error. Wrong type {fieldType.ToString()} should be bool");
                return(null);
            }

            _checkbox = new Checkbox
            {
            };

            _checkbox.SetBinding(Checkbox.CheckedProperty, new Binding(bindingName, BindingMode.TwoWay, new NullableConverter(), fieldType));

            return(_checkbox);
        }
        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   = attribute.Placeholder,
                        HeightRequest = attribute.HeightRequest,
                    };
                    iv.SetBinding(Editor.TextProperty, new Binding(property.Name, BindingMode.TwoWay));
                }
                else
                {
                    iv = new Entry
                    {
                        Style       = style,
                        Placeholder = 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 ImageButton
                {
                    Style             = style,
                    Text              = btnAttrib?.Text,
                    HorizontalOptions = LayoutOptions.CenterAndExpand,
                    VerticalOptions   = LayoutOptions.CenterAndExpand,
                };
                v.SetBinding(ImageButton.CommandParameterProperty, new Binding("."));
                v.SetBinding(ImageButton.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 = "--", Value = -1
                    });

                    int index = 0;

                    foreach (var d in dict)
                    {
                        if (d.Value != "Unspecified" && d.Key != -1)
                        {
                            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.Start,
                    HorizontalOptions       = LayoutOptions.StartAndExpand,
                    HorizontalTextAlignment = attribute.HorizontalItemAlignment,
                    LineBreakMode           = LineBreakMode.TailTruncation,
                    MaxLines                = 1,
                };
                v.SetBinding(Label.TextProperty, new Binding(property.Name, converter: new DisplayConverter()));
                break;
            }

            return(v);
        }