Hello. I'm creating "image buttons" in code-behind dynamically which involve and outer frame. Inside the frame is a stacklayout. Inside the stacklayout is 1) an image and 2) a label. Here are two examples:
What I need to do now is in the code-behind, change the image source of a specific "image button" which is tapped. I have the tapped event tied to the outermost element, the frame, so user doesn't have to click exactly on the image, but anywhere within the frame which appears to be the entire button.
Here is my code:
public static Frame CreateButton(string imgUrl, string name, Color bgColor)
{
Frame fr = new Frame
{
BorderColor = Color.Transparent,
CornerRadius = 10,
BackgroundColor = bgColor,
HeightRequest = 150,
WidthRequest = 100,
Margin = new Thickness(2, 2, 2, 2)
};
StackLayout sl = new StackLayout();
sl.Margin = new Thickness(0, 0, 0, 0);
sl.Orientation = StackOrientation.Vertical;
sl.HorizontalOptions = LayoutOptions.FillAndExpand;
sl.VerticalOptions = LayoutOptions.FillAndExpand;
Image img = new Image
{
Aspect = Aspect.AspectFit,
HeightRequest = 45,
WidthRequest = 45,
HorizontalOptions = LayoutOptions.Center,
Source = imgUrl
};
Label lab = new Label
{
Text = name,
TextColor = Color.White,
FontSize = 16,
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.Center
};
sl.Children.Add(img);
sl.Children.Add(lab);
fr.Content = sl;
return fr;
}