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

1

6

I use an <Frame> in my xaml page like this:

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

But I want to do the same thing on the C# side.

I define the StackLayout, Label and Button.

Now I can add the Button to the StackLayout using Children, but I cannot add the StackLayout to the Frame because it doesn't have a Children property.

Is it possible to add a StackLayout inside a Frame using C# as background code ?

Radicle answered 17/10, 2017 at 6:41 Comment(0)
L
22

A frame has a unique property: Content. Follow this snippet to create your UI in code behind:

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

Moreover, if you want to have multiple children in your Frame, wrap it in an unique Container (Grid, RelativeLayout, AbsoluteLayout, ...)

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

Lenes answered 17/10, 2017 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.