简体   繁体   中英

Get Entry.Text from Entry defined in xaml

trying to write calmly as I am being seriously frustrated by Xamarin right now. I have an Entry that is defined in my MainPage.xaml. It looks like this:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:TestFood"
         x:Class="TestFood.MainPage" BackgroundColor="CornflowerBlue">

<ContentPage.Content>
    <StackLayout VerticalOptions="CenterAndExpand">
        <Label HorizontalTextAlignment="Center" Text="EATH" TextColor="#FFFFFF" FontSize="64"/>
        <StackLayout>
            <Entry x:Name="budgetEntry"  Placeholder="Budget" Keyboard="Numeric" HorizontalTextAlignment="Center" Margin="40,0"/>
            <Button Margin="80,0" Text="Search Recipes" Clicked="Button_Clicked">SubmitButton</Button>
        </StackLayout>
    </StackLayout>
</ContentPage.Content>

All I'm trying to do is take the text from the Entry and use it in my MainPage.xaml.cs. Seeing as Xamarin can be quite convoluted, how do I go about this? I've looked at Binding but the documentation is rather poor and doesn't really get you started anywhere.

任何分配了 Name 的元素都可以从后面的代码中引用

var txt = budgetEntry.Text;

While accessing the Text property from code behind is an option, just like Jason mentioned in his answer, binding seems a better and more solid option to go for in my opinion.

An interesting article posted by Xamarin itself explains this quite well. You can read it here: https://blog.xamarin.com/introduction-to-data-binding/

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