0

有没有一种方法可以添加到已经创建StackLayout的而不需要调用多个Children.Add

这是已创建的代码:

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" });
};

请注意,对于这个问题,我不想{ }在新的Stacklayout() { }

4

1 回答 1

1

这一切都取决于:

如果你已经有一个集合,你可以做类似的事情

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);
};

除此之外,我不相信你可以......

于 2020-11-26T09:55:27.067 回答