如何在Xamarin Forms中单击提交按钮后从XAML获取价值?

时间:2022-10-17 20:31:54

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

我想在点击一个事件后从Entry输入中检索一个值。我该如何检索它。以下是我的XAML代码:

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

我想在submit_Clicked()方法中检索数据。

      private void submit_Clicked(object sender, EventArgs e)
        {
how?
        }

3 个解决方案

#1


3  

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

您可以从Entry.Text属性获取值。例如:

private void submit_Clicked(object sender, EventArgs e)
{
     var nameValue = name.Text;
}

#2


1  

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

#3


-4  

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

#1


3  

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

您可以从Entry.Text属性获取值。例如:

private void submit_Clicked(object sender, EventArgs e)
{
     var nameValue = name.Text;
}

#2


1  

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

#3


-4  

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