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

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 ?



Solution 1:[1]

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/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Junaid Pathan