将下拉选择器连接到数据库

问题描述

我正在做一个项目,我必须创建一个注册页面,在该页面中我必须有一个下拉选择器,以便用户可以选择他们所在的国家/地区。我正在按照我在 youtube 上找到的这个视频中的一些步骤进行操作:https://www.youtube.com/watch?v=E_0XDyy4ef8 和有关 udemy 的课程,但他们没有介绍如何创建后端代码以将其连接到数据库的步骤。如果有人能帮助我,那就太好了!

xml-

<Picker ItemdisplayBinding="{Binding CountryName}" 
                        x:Name="EntCountry" 
                        Title="Select Country" 
                        ItemsSource="{Binding CountList}" 
                        TextColor="White" 
                        TitleColor="White"/>

cs-

 private async void BtnSignUp_OnClicked(object sender,EventArgs e)
        {
            var response = await ApiService.RegisterUser(EntName.Text,EntEmail.Text,EntPassword.Text,EntConfirmPassword.Text,Entdob.Text,EntCountry);
                if (response)
            {
                await displayAlert("Hello!","Your account has been created","Okay");
                await Navigation.PushModalAsync(new LoginPage()); //This will transition to another screen. 
            }
            else
            {
                await displayAlert("Hello!","There is something wrong","Okay");
            }
        }

注册.cs

class Register
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string ConfirmPassword { get; set; }
        public string Country { get; set; }
        public string dob { get; set; }
    }

ApiService.cs-

public static async Task<bool> RegisterUser(string name,string email,string password,string confirmpassword,string dob,string country)
        {
            var register = new Register()
            {
                Name = name,Email = email,Password = password,ConfirmPassword = confirmpassword,Country = country,dob = dob,

Country.cs-

public class Country
    {
        public int CountryId { get; set; }
        public string CountryName { get; set; }
    }

Countryviewmodel.cs-

class Countryviewmodel
    {
        public IList<Country> CountList { get; set; }

        public Countryviewmodel()
        {
            try
            {
                CountList = new ObservableCollection<Country>();
                CountList.Add(new Country { CountryId = 1,CountryName = "United States" });
                CountList.Add(new Country { CountryId = 2,CountryName = "Ecuador" });
                CountList.Add(new Country { CountryId = 3,CountryName = "Brazil" });
                CountList.Add(new Country { CountryId = 4,CountryName = "Argentina" });
                CountList.Add(new Country { CountryId = 5,CountryName = "Colombia" });
                CountList.Add(new Country { CountryId = 6,CountryName = "Peru" });
                CountList.Add(new Country { CountryId = 7,CountryName = "Chile" });
                CountList.Add(new Country { CountryId = 8,CountryName = "Venezuela" });
                CountList.Add(new Country { CountryId = 9,CountryName = "Bolivia" });
                CountList.Add(new Country { CountryId = 10,CountryName = "Uruguay" });
                CountList.Add(new Country { CountryId = 11,CountryName = "Paraguay" });
                CountList.Add(new Country { CountryId = 12,CountryName = "Mexico" });
            }
            catch(Exception)
            {

            }
        }
    }

解决方法

您是否为 VM 使用了 BindingContext?看看this。作为建议,如果您使用 VM,请尝试将单击按钮的代码放入其中。