Reputation: 31
Im having troubles retrieving textbox input in Xamarin using XAML and C#.
I want the user to enter their ID in a field, and after hitting the submit button, I can retrieve the value in the field.
Here's what I have so far:
XAML:
<Entry x:Name="UserInput"
Placeholder="Enter employee ID"
PlaceholderColor="Gray"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Button Text="Submit"
VerticalOptions="Center"
Clicked="GetUserInput"/>
Xaml.CS:
void GetUserInput(System.Object sender, System.EventArgs e)
{
var matchingPerson = UserInput.Text;
Console.WriteLine(matchingPerson);
}
public MainPage()
{
InitializeComponent();
}
However, when I call, matchingPerson = UserInput.Text, I can't access the value of UserInput. It's in a separate file and since its XAML there is no "namespace" for me to access it by.
How do I go about fetching the value of the UserInput textbox after users have entered their data in the field?
Upvotes: 0
Views: 798
Reputation: 31
For whatever the reason, closing out of the solution and reopening it allowed it to run. Xamarin was somehow reporting a false error to me in the console. The code above is correct - Xamarin was being buggy. I forced quit all of my visual studio applications in activity monitor and opened them back up - that did the trick.
Upvotes: 0
Reputation: 41
Your code is working fine in my side it's the issue in xamarin.forms 4.7 to get x:name in code behind
My xamarin.forms is 4.5.0.495
Xmal:
<StackLayout VerticalOptions="CenterAndExpand">
<Entry x:Name="UserInput"
Placeholder="Enter employee ID"
PlaceholderColor="Gray"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Button Text="Submit"
VerticalOptions="Center"
Clicked="GetUserInput"/>
</StackLayout>
Code behind:
void GetUserInput(System.Object sender, System.EventArgs e)
{
var matchingPerson = UserInput.Text;
Console.WriteLine(matchingPerson);
}
Upvotes: 0