使用 C# 作为后台代码在 Xamarin Forms 的框架内创建 StackLayout

Create a StackLayout inside a Frame in Xamarin Forms using C# as background code

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

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

但我想在 C# 端做同样的事情。

我定义了StackLayoutLabelButton

现在我可以使用 ChildrenButton 添加到 StackLayout,但我无法将 StackLayout 添加到 Frame,因为它没有没有 Children 属性。

是否可以使用 C# 作为后台代码在 Frame 中添加 StackLayout

一个框架有一个独特的属性:内容。按照此代码段在后面的代码中创建您的 UI:

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

此外,如果您想在您的框架中包含多个 children,请将其包装在一个独特的容器中(Grid、RelativeLayout、AbsoluteLayout,...)

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