Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 958
Mahmut Bedir Avatar asked Nov 29 '22 22:11

Mahmut Bedir


1 Answers

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/

like image 138
Rudy Spano Avatar answered Dec 04 '22 21:12

Rudy Spano