简体   繁体   中英

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?
        }

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

private void submit_Clicked(object sender, EventArgs e)
{
     var nameValue = name.Text;
}
 private void submit_Clicked(object sender, EventArgs e)
  {
    Entry entry = e as Entry;
     var text=entry.Text;
   }
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;
}

Below how you can do it, you might need to cast it first.

private void submit_Clicked(object sender, EventArgs e)
{
   Entry entry = sender as Entry;
   var text=entry.Text;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM