Problem with C# layout

MeccieMeccie USMember ✭✭

Hey Developer!

yesterday i made a post where i asked how to add elements to an excisting layout, now i have another problem :sweat:

i can only add one label to a stacklayout when i do this:

            foreach (var user in werknemer._werknemers)
            {
                StackLayoutPeople.Children.Add(new Frame
                {
                        Content = new Label { Text = user.Voornaam }

                });

            }

i seem to get an error everytime i try to add something :sweat:
(i'm a rookie when it comes to C# layout)

Best Answer

Answers

  • NMackayNMackay GBInsider, University ✭✭✭✭✭
    edited November 10

    @Meccie

    What error? you need to provide more detail if you want people to assist you.

    The code below works fine

    public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
    
                for (var i = 0; i < 20; i++)
                {
                    StackLayoutPeople.Children.Add(new Frame
                    {
                        Content = new Label { Text = "label " +i }
    
                    });
                }
            }
        }
    

    Is _werknemers initialized correctly when that code runs?

  • MeccieMeccie USMember ✭✭
    edited November 10

    @NMackay no i mean this:

                foreach (var user in werknemer._werknemers)
                {
                    StackLayoutPeople.Children.Add(new Frame
                    {
                        Content = new Label
                        {
                            Text = user.Voornaam
                        }                       
    
                        Content = new Label
                        {
                            Text = user.Achternaam
                        }
    
                    });
    
                }
    

    i want to add multiple labels in one frame.

    but this throws an error how do i add multiple lables to a single (excisting) frame

  • JohnHardmanJohnHardman GBUniversity ✭✭✭✭✭

    @Meccie - A Frame can only have one Content. A StackLayout has Children, of which there can be multiple. Your code is trying to add two Contents to a Frame (not allowed) instead of two Frames to a StackLayout.

  • MeccieMeccie USMember ✭✭

    @JohnHardman so i cannot add multiple labels to one frame?

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    @Meccie

    It's worth reading up on layout controls in Xamarin Forms before diving in too deep, once you understand then it makes life a lot easier

    https://developer.xamarin.com/guides/xamarin-forms/user-interface/controls/layouts/

    For example the frame overview text reveals the issue you were having

    An element containing a single child, with some framing options. Frame have a default

Sign In or Register to comment.