如何解决将下拉选择器连接到数据库
我正在做一个项目,我必须创建一个注册页面,在该页面中我必须有一个下拉选择器,以便用户可以选择他们所在的国家/地区。我正在按照我在 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,请尝试将单击按钮的代码放入其中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。