I have a template frame that provides some padding and accepts multiple elements:
[Xamarin.Forms.ContentProperty("Contents")]
public class ContentFrame : StackLayout
{
public StackLayout ContentStack = new StackLayout();
public IList<View> Contents { get => ContentStack.Children; }
public ContentFrame()
{
CustomFrame cf = new CustomFrame()
{
Content = ContentStack,
HasShadow = false,
};
cf.SetDynamicResource(BackgroundColorProperty, "ContentFrameBackgroundColor");
cf.SetDynamicResource(Frame.CornerRadiusProperty, "ContentFrameCornerRadius");
cf.SetDynamicResource(MarginProperty, "ContentFrameMargin");
this.Children.Add(cf);
}
I would like to add child labels like this: c1.Children.Add - But when I do this the BackgroundColor, CornerRadius and Margin don't get used (see first part of image for ABC and ABC)
The only way I can get it to use these is by exposing ContentStack as a public property and by adding to that (see below for ABC and GHI)
public class TestPage : HeadingView
{
public TestPage() : base()
{
var s = new Stack();
var c1 = new ContentFrame();
c1.Children.Add(new Label() { Text = "ABC" });
c1.Children.Add(new Label() { Text = "DEF" });
var c2 = new ContentFrame();
c2.ContentStack.Children.Add(new Label() { Text = "DEF" });
c2.ContentStack.Children.Add(new Label() { Text = "GHI" });
s.Children.Add(c1);
s.Children.Add(c2);
this.InnerContent = s;
}
}
Question > Can anyone explain why the first case (with get => ContentStack.Children) doesn't show the frame background, radius etc.
Add
method on the class that does what you want – Jason