How to add to an already created StackLayout without multiple: Children.Add?

Issue

Is there a way I can add to an already created StackLayout without the need of calling multiple Children.Add?

Here is the code that’s been created:

public class Test1
{
    public StackLayout contentStack;

    public PopupDialog()
    {
        contentStack  new StackLayout()
        {
            Spacing  0,
            Padding  new Thickness(0),
            Orientation  StackOrientation.Vertical,

        };
        contentStack.Children.Add(new Label() { Text  "Test1" });
        contentStack.Children.Add(new Label() { Text  "Test2" });
        contentStack.Children.Add(new Label() { Text  "Test3" });
};

Note that for this question I don’t want to add within the { } of the new Stacklayout() { }

Solution

It all depends:

If you already have a collection you can do something like

public class Test1
{
    List<Label> list  new List<Label>()
    {
        new Label() { Text  "Test1" },
        new Label() { Text  "Test2" },
        new Label() { Text  "Test3" },
    };

    public StackLayout contentStack;

    public PopupDialog()
    {
        contentStack  new StackLayout()
        {
            Spacing  0,
            Padding  new Thickness(0),
            Orientation  StackOrientation.Vertical,

        };

        foreach(var item in list)
            contentStack.Children.Add(item);
};

Other than that, i do not believe you can…

Answered By – Mönchskopf

Leave a Comment