public View CreateButton(Func <string, Task> asycFunction, double width, double height, string text, string image = null, double?fontSize = null, Color?backColorOverride = null, AlignImageEnumeration alignImage = AlignImageEnumeration.Left) { var result = CreateButtonView(width, height, text, image, fontSize, backColorOverride, alignImage); if (alignImage == AlignImageEnumeration.Left) { AddTapGestureToView(result.layout, asycFunction, text); } else { AddTapGestureToView(result, asycFunction, text); } return(result.layout); }
private ImageButtonContents CreateButtonView(double width, double height, string text, string image = null, double?fontSize = null, Color?backColorOverride = null, AlignImageEnumeration alignImage = AlignImageEnumeration.Left) { backColorOverride = backColorOverride.HasValue ? backColorOverride : _backColor; StackLayout layout = new StackLayout() { Margin = new Thickness(5, 5, 5, 5), Padding = new Thickness(8, 8, 8, 8), BackgroundColor = backColorOverride.Value, WidthRequest = width, HeightRequest = height, Orientation = StackOrientation.Horizontal, AutomationId = $"imgBtn{text}" //Used to prevent duplicates }; if (text.IsNullOrEmpty()) { //No text, just an image, center in on the 'button' layout.VerticalOptions = LayoutOptions.CenterAndExpand; layout.HorizontalOptions = LayoutOptions.CenterAndExpand; layout.Padding = new Thickness(2, 2, 2, 2); } Image img = null; if (image.IsNotNullOrEmpty()) { img = new Image() { Source = image, WidthRequest = text.IsNotNullOrEmpty()? width / 5 : width, HeightRequest = text.IsNotNullOrEmpty()? height / 1.8 : height, BackgroundColor = Color.Transparent }; } Label lbl = null; if (text.IsNotNullOrEmpty()) { lbl = new Label() { TextColor = _textColor, Text = $" {text.ToUpper()}", FontSize = fontSize ?? height / 2.2, BackgroundColor = backColorOverride.Value, HeightRequest = height - 10, VerticalTextAlignment = TextAlignment.Center }; } if (alignImage == AlignImageEnumeration.Left && img != null) { layout.Children.Add(img); } if (lbl != null) { layout.Children.Add(lbl); } if (alignImage == AlignImageEnumeration.Right && img != null) { StackLayout alignRight = new StackLayout() { Padding = new Thickness(0, 10, 0, 0), HorizontalOptions = LayoutOptions.EndAndExpand, Children = { img } }; layout.Children.Add(alignRight); layout.Padding = 0; layout.Margin = .5; } return(new ImageButtonContents() { lbl = lbl, layout = layout, img = img }); }
public static void AddButton(StackLayout container, Func <string, Task> asyncFunction, double width, double height, string text = null, string image = null, double?fontSize = null, Color?backColor = null, AlignImageEnumeration alignImage = AlignImageEnumeration.Left) { //We don't add duplicates to the stacklayout if (container.Children.All(c => c.AutomationId != $"imgBtn{text}")) { var bg = ImgBtnGenerator.GetInstance(); var button = bg.CreateButton(asyncFunction, width, height, text, image, fontSize, backColor, alignImage); container.Children.Add(button); } }