Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
        }
like image 659
Susen Maharjan Avatar asked Oct 17 '25 11:10

Susen Maharjan


2 Answers

You can get value from Entry.Text property. For example:

private void submit_Clicked(object sender, EventArgs e)
{
     var nameValue = name.Text;
}
like image 129
Andrii Krupka Avatar answered Oct 19 '25 01:10

Andrii Krupka


 private void submit_Clicked(object sender, EventArgs e)
  {
    Entry entry = e as Entry;
     var text=entry.Text;
   }
like image 31
Prashant Bamania Avatar answered Oct 19 '25 00:10

Prashant Bamania



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!