【问题标题】:Picker item value insert to database(firebase) in xamarin form选择器项目值以 xamarin 形式插入到数据库(firebase)
【发布时间】:2022-12-01 10:33:54
【问题描述】:

我的问题是以 xamarin 形式将数据插入到 firebase。 这是我的 CreateAccount.xaml `

<Picker 
                                Title="Select Security Question" 
                                TextColor="Black"
                                HorizontalOptions="StartAndExpand"
                                FontSize="Medium"
                                WidthRequest="250"
                                TitleColor="Black"
                                x:Name="entryField_SecurityQuestion"
                                >
                                <Picker.Items 
                                
                                    >
                                
                                    <x:String>
                                        Who is your first love?
                                    </x:String>
                                    <x:String>
                                       what subject you failed?
                                    </x:String>
                                     <x:String>
                                       What is the name of your dog?
                                    </x:String>
                                     <x:String>
                                       Who is your first kiss?
                                    </x:String>
                                    
                                </Picker.Items>
                                
                            </Picker>

codebehind.cs

async public void signButton_Clicked(System.Object sender, System.EventArgs e)
        {

           Picker picker = sender as Picker;

            //this is how I manage my some ID from my front end
            string firstName = entryField_Firstname.Text;
            string lastName = entryField_Lastname.Text;
            string contactNumber = entryField_PhoneNumber.Text;


             string securityQuestion = picker.SelectedItem.ToString(); // this line is my trial and error, I tried some code but I cant insert data to database

            Customer customer = new Customer();
            customer.FirstName = firstName;
            customer.LastName = lastName;
            customer.SecurityQuestion = securityQuestion;//this one also, this is my trial and error approach 
            customer.ContactNumber = contactNumber;

            var Savedata = await customerRepo.Save(customer);
         }

`

我希望,我可以获得选择器的项目,并将其保存到数据库中。 将发布与我的帖子相关的任何链接,将不胜感激。太感谢了

【问题讨论】:

  • 按钮点击事件的senderButton而不是Picker。您已经为您的 `Picker 分配了一个名称,所以只需使用该名称来引用它

标签: firebase xamarin.forms picker


【解决方案1】:

如果你想获得 Picker 的SelectedItem,你需要参考你的 picker 的x:Name="entryField_SecurityQuestion"而不是Picker picker = sender as Picker;,正如 Jason 所说。

您可以参考以下代码:

        private void signButton_Clicked(object sender, EventArgs e) 
    { 
       // remove the following code
        //Picker picker = sender as Picker;
         
        string securityQuestion = entryField_SecurityQuestion.SelectedItem.ToString();

        Customer customer = new Customer();
        customer.FirstName = firstName;
        customer.LastName = lastName;
        customer.SecurityQuestion = securityQuestion;

    }

【讨论】:

    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    相关资源
    最近更新 更多