首页 > 解决方案 > 通过 c# xamarin 表单将孙子添加到堆栈布局

问题描述

我有这个代码:

stackRecentList.Children.Add(
                    new Frame {
                        BackgroundColor = Color.White,
                        Margin = new Thickness(30, 20, 30, 0),
                        Padding = new Thickness(10),
                        CornerRadius = 5,
                        HasShadow = true
                    }
                );

基本上我正在向<Frame>我的堆栈布局添加一个。我想在那里添加一个<Label><Frame>因为它成为<StackLayout>的孙子。如何在 Xamarin Forms Android 中执行此操作?

标签: c#xamarin.forms

解决方案


一般来说,aStackLayout只有它Children需要照顾,现在每个孩子的任务都是组织自己的孩子。现在你的框架必须自己照顾它的孩子。

Frame 有一个独特的属性:Content.

按照此代码段在后面的代码中创建您的 UI:

 Frame frame = new Frame
            {
                BackgroundColor = Color.White,
                Margin = new Thickness(30, 20, 30, 0),
                Padding = new Thickness(10),
                CornerRadius = 5,
                HasShadow = true
            };

            var sl = new StackLayout();
            sl.Children.Add(new Label() { Text = "test" });

            frame.Content = sl;


            stackRecentList.Children.Add(
                    frame
                );

我建议您在 Frame 中添加一个StackLayoutContent不要直接添加Label到 Frame 中Content,如果您有其他控件想稍后添加 Frame)。

这是运行截图。

在此处输入图像描述


推荐阅读