使用C#作为背景代码,在Xamarin Forms中创建一个位于Frame内的StackLayout

6

我在我的xaml页面中使用了一个<Frame>,如下所示:

<StackLayout>
    <Frame>
        <StackLayout>
            <Button></Button>
            <Label></Label>
        </StackLayout>
        <StackLayout>
            <Label></Label>
        </StackLayout>
    </Frame>
</StackLayout>

但我希望在 C# 方面做同样的事情。
我定义了 StackLayout,Label 和 Button。
现在我可以使用 Children 将 Button 添加到 StackLayout 中,但我无法将 StackLayout 添加到 Frame 中,因为它没有 Children 属性。
是否可能在 C# 后台代码中将 StackLayout 添加到 Frame 中?
1个回答

22

框架有一个独特的属性:Content。请按照以下代码片段在代码后端创建您的用户界面:

var stackLayout = new StackLayout();
//add content...
stackLayout.Children.Add(new Label);
var frame = new Frame;
frame.Content = stackLayout

另外,如果您想在Frame中拥有多个子元素,请将其放入唯一的容器(Grid、RelativeLayout、AbsoluteLayout等)中。

请参见https://developer.xamarin.com/api/type/Xamarin.Forms.Frame/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接