Rajdeep
Rajdeep

Reputation: 2482

Error when using Children.Add in Xamarin Forms

Im having some problems with the Children.Add() function of a Stack Layout. There is an error in the code below which says " There is no argument that corresponds to the required formal parameter 'value' of 'SettersExtentions.Add(IList, Bindable Property, object)' ". The error is a curly red underline under all the .Add words.

public class LoginPage : ContentPage
{
    public string email { get; set; }
    public string password { get; set; }
    Entry emailBox = new Entry();
    Entry passwordBox = new Entry();
    Button createAccount = new Button();
    Button forgotPassword = new Button();
    Layout layout = new StackLayout();


    public LoginPage()
    {
        Title = "Login";
        emailBox.Placeholder = "email";
        passwordBox.Placeholder = "password";
        passwordBox.IsPassword = true;

        createAccount.Text = "Create Account";
        createAccount.Font = Font.SystemFontOfSize(NamedSize.Large);
        createAccount.BorderWidth = 1;
        createAccount.HorizontalOptions = LayoutOptions.Center;
        createAccount.VerticalOptions = LayoutOptions.CenterAndExpand;

        forgotPassword.Text = "Forgot Password";
        forgotPassword.Font = Font.SystemFontOfSize(NamedSize.Large);
        forgotPassword.BorderWidth = 1;
        forgotPassword.HorizontalOptions = LayoutOptions.Center;
        forgotPassword.VerticalOptions = LayoutOptions.CenterAndExpand;

        layout.VerticalOptions = LayoutOptions.CenterAndExpand;

        layout.Children.Add(emailBox);
        layout.Children.Add(passwordBox);
        layout.Children.Add(createAccount);
        layout.Children.Add(forgotPassword);

        Content = layout;

    }

    public void Check(string email, string password, string cpassword)
    {

    }
}

Upvotes: 2

Views: 307

Answers (1)

Nkosi
Nkosi

Reputation: 247451

Layout.Children is a readonly IReadOnlyList<Element> so you wont be able to add to it.

However StackLayout.Children is IList<T> which will allow you to add children

change

Layout layout = new StackLayout();

to

StackLayout layout = new StackLayout();

This will allow you to be able to add the child elements to the layout.

Upvotes: 3

Related Questions