在使用XAML和C#检索Xamarin的文本框输入时,我遇到了困难。
我希望用户在字段中输入他们的ID,在点击submit按钮后,我可以检索字段中的值。
到目前为止,我的情况如下:
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();
        }但是,当我调用matchingPerson = UserInput.Text时,我无法访问UserInput的值。它位于一个单独的文件中,因为它的XAML没有“名称空间”供我访问。
如何在用户在字段中输入数据后获取UserInput文本框的值?
发布于 2020-06-30 10:57:54
您的代码在我这边运行得很好,这是xamarin.forms 4.7中的问题,需要在后面的代码中获得x:name
我的xamarin.forms是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>代码背后:
void GetUserInput(System.Object sender, System.EventArgs e)
    {
        var matchingPerson = UserInput.Text;
        Console.WriteLine(matchingPerson);
    }发布于 2020-06-30 11:39:18
无论出于什么原因,关闭解决方案并重新打开它,都允许它运行。不知何故,Xamarin在控制台里向我报告了一个错误。上面的代码是正确的我强制退出了活动监视器中的所有visual应用程序,并重新打开它们--这就成功了。
https://stackoverflow.com/questions/62642236
复制相似问题