コード例 #1
0
        AutoFormsButtonAttribute FindButtonAttribute(string commandName, ControlList listControl)
        {
            var domainModelType = listControl.BindingContext.GetType();

            var props = AttributeHelper.GetPropertyAttributes <AutoFormsButtonAttribute>(domainModelType);

            foreach (var p in props)
            {
                if (p.Item1.Name != commandName || p.Item2 == null)
                {
                    continue;
                }

                return(p.Item2[0]);
            }

            return(null);
        }
コード例 #2
0
        public static ControlBase CreateControl(ControlConfig config)
        {
            var attribute = config.Attribute;
            var property  = config.Property;

            ControlBase item = null;

            switch (attribute.Type)
            {
            case AutoFormsType.Group:
                item = new ControlGroup(config);
                break;

            case AutoFormsType.Custom:
                var attribCustom = property.GetAttribute <AutoFormsCustomAttribute>();
                if (attribCustom != null && string.IsNullOrEmpty(attribCustom.CustomControlType) == false)
                {
                    item = CreateCustomControl(attribCustom.CustomControlType);

                    if (item != null && item is ControlCustom customControl)
                    {
                        customControl.InitializeCustom(config);
                    }
                }
                break;

            case AutoFormsType.ActionList:
                var attribList = property.GetAttribute <AutoFormsListAttribute>();
                if (attribList != null)
                {
                    item = new ControlList(config);
                }
                break;

            case AutoFormsType.Entry:
                item = new ControlEditor(config);
                break;

            case AutoFormsType.Combo:
                item = new ControlCombo(config);
                break;

            case AutoFormsType.Checkbox:
                item = new ControlCheckbox(config);
                break;

            case AutoFormsType.Radio:
                item = new ControlRadio(false, config);
                break;

            case AutoFormsType.DateTime:
                item = new ControlDateTime(config);
                break;

            case AutoFormsType.Button:
                item = new ControlButton(config);
                break;

            case AutoFormsType.Label:
                item = new ControlLabel(config);
                break;

            case AutoFormsType.SelectButton:
                item = new ControlSelectButton(config);
                break;

            case AutoFormsType.Auto:

                var p = property.PropertyType;

                var t = Nullable.GetUnderlyingType(p);
                if (t != null)
                {
                    p = t;
                }

                switch (p)
                {
                case Type _ when p == typeof(int):
                case Type _ when p == typeof(float):
                case Type _ when p == typeof(double):
                case Type _ when p == typeof(decimal):
                case Type _ when p == typeof(string):
                    item = new ControlEditor(config);
                    break;

                case Type _ when p == typeof(DateTime):
                case Type _ when p == typeof(DateTimeOffset):
                    item = new ControlDateTime(config);
                    break;

                case Type _ when p == typeof(ICommand):
                    item = new ControlButton(config);
                    break;

                case Type _ when p == typeof(bool):
                    item = new ControlCheckbox(config);
                    break;

                case Type _ when p.IsEnum:
                    item = new ControlCombo(config);
                    break;

                case Type _ when p == typeof(object):
                    item = new ControlLabel(config);
                    break;
                }
                break;
            }

            return(item);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        void CreateActionButtons(ControlList listControl)
        {
            if (listControl == null || listControl.HasActions == false)
            {
                return;
            }

            Style buttonStyle = null;

            if (Application.Current.Resources.TryGetValue(AutoFormsConstants.ActionButtonStyle, out object obj))
            {
                buttonStyle = (Style)obj;
            }

            // action buttons
            _grid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(ControlList.ActionButtonWidth, GridUnitType.Absolute)
            });

            var stack = new StackLayout
            {
                HorizontalOptions = LayoutOptions.EndAndExpand,
                Orientation       = StackOrientation.Horizontal,
                Spacing           = 15,
            };

            Grid.SetColumn(stack, _grid.ColumnDefinitions.Count - 1);
            _grid.Children.Add(stack);

            if (string.IsNullOrEmpty(listControl.EditCommand) == false)
            {
                var attrib = FindButtonAttribute(listControl.EditCommand, listControl);

                var style = buttonStyle;
                var text  = AutoFormsConstants.Button_Edit;

                if (attrib != null)
                {
                    if (Application.Current.Resources.TryGetValue(attrib.ButtonStyle, out object obj1))
                    {
                        style = (Style)obj1;
                    }
                    text = attrib.Text;
                }

                var b = new Button
                {
                    Style      = style,
                    Text       = text,
                    FontFamily = AutoFormsConstants.FontFamilyDefault,
                };

                b.SetBinding(ImageButton.CommandParameterProperty, new Binding("."));
                b.SetBinding(ImageButton.CommandProperty, new Binding("BindingContext." + listControl.EditCommand, source: listControl));
                stack.Children.Add(b);
            }
            if (string.IsNullOrEmpty(listControl.ViewCommand) == false)
            {
                var attrib = FindButtonAttribute(listControl.ViewCommand, listControl);

                var style = buttonStyle;
                var text  = AutoFormsConstants.Button_View;

                if (attrib != null)
                {
                    if (Application.Current.Resources.TryGetValue(attrib.ButtonStyle, out object obj2))
                    {
                        style = (Style)obj2;
                    }
                    text = attrib.Text;
                }

                var b = new Button
                {
                    Style      = style,
                    Text       = text,
                    FontFamily = AutoFormsConstants.FontFamilyDefault,
                };
                b.SetBinding(ImageButton.CommandParameterProperty, new Binding("."));
                b.SetBinding(ImageButton.CommandProperty, new Binding("BindingContext." + listControl.ViewCommand, source: listControl));
                stack.Children.Add(b);
            }
            if (string.IsNullOrEmpty(listControl.DeleteCommand) == false)
            {
                var attrib = FindButtonAttribute(listControl.DeleteCommand, listControl);

                var style = buttonStyle;
                var text  = AutoFormsConstants.Button_Delete;

                if (attrib != null)
                {
                    if (Application.Current.Resources.TryGetValue(attrib.ButtonStyle, out object obj3))
                    {
                        style = (Style)obj3;
                    }
                    text = attrib.Text;
                }

                var b = new Button
                {
                    Style      = style,
                    Text       = text,
                    FontFamily = AutoFormsConstants.FontFamilyDefault,
                };
                b.SetBinding(ImageButton.CommandParameterProperty, new Binding("."));
                b.SetBinding(ImageButton.CommandProperty, new Binding("BindingContext." + listControl.DeleteCommand, source: listControl));
                stack.Children.Add(b);
            }
        }