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); }
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; } break; } return(item); }
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); }
void CreateActionButtons(AutoFormsListUIAttribute attribute, ControlList listControl) { if (listControl == null || attribute == null || listControl.HasActions == false) { return; } Style buttonStyle = null; if (Application.Current.Resources.TryGetValue(attribute.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 = "\uE70F"; if (attrib != null) { if (Application.Current.Resources.TryGetValue(attrib.ButtonStyle, out object obj1)) { style = (Style)obj1; } text = attrib.Text; } var b = new ImageButton { Style = style, Text = text, Tooltip = "Edit" }; 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 = "\uE890"; if (attrib != null) { if (Application.Current.Resources.TryGetValue(attrib.ButtonStyle, out object obj2)) { style = (Style)obj2; } text = attrib.Text; } var b = new ImageButton { Style = style, Text = text, Tooltip = "View" }; 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 = "\uE74D"; if (attrib != null) { if (Application.Current.Resources.TryGetValue(attrib.ButtonStyle, out object obj3)) { style = (Style)obj3; } text = attrib.Text; } var b = new ImageButton { Style = style, Text = text, Tooltip = "Delete" }; b.SetBinding(ImageButton.CommandParameterProperty, new Binding(".")); b.SetBinding(ImageButton.CommandProperty, new Binding("BindingContext." + listControl.DeleteCommand, source: listControl)); stack.Children.Add(b); } }