How to get value from XAML after clicking a submit button in Xamarin Forms?

Viewed 23524

I want to retrieve a value from the Entry input after clicking an event. How can I retrieve it. Following is my XAML code:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:HelloWorld"
             x:Class="HelloWorld.MainPage">

    <StackLayout Spacing="10" x:Name="layout">

        <Label Text="Name" VerticalOptions="Start" FontSize="Large"/>

        <Entry x:Name="name" Placeholder="Enter your name"/>


        <Label Text="Password" VerticalOptions="Start" FontSize="Large"/>

        <Entry x:Name="password" IsPassword="True" Placeholder="****"/>

        <Label Text="Date of Birth" VerticalOptions="Start" FontSize="Large"/>
        <DatePicker x:Name="dob"/>

        <Label Text="Gender" VerticalOptions="Start" FontSize="Large"/>

        <Picker x:Name="GenderPicker" HorizontalOptions="FillAndExpand">
            <Picker.Items>
                <x:String>Male</x:String>
                <x:String>Female</x:String>
            </Picker.Items>
        </Picker>

        <Button x:Name="submit" Text="Save" BorderWidth="20" Clicked="submit_Clicked"/>

    </StackLayout>

</ContentPage>

And i want to retrieve the data in a submit_Clicked() method.

      private void submit_Clicked(object sender, EventArgs e)
        {
how?
        }
4 Answers
private void submit_Clicked(object sender, EventArgs e)
{
    var entry_name = new Entry {Placeholder= "name" };//for getting value of Entry name
    var entry_password = new Entry {Placeholder ="password" };
    var text=entry_name.Text;
    var password= entry_password.Text;
}
Related