将下拉选择器连接到数据库
Posted
技术标签:
【中文标题】将下拉选择器连接到数据库【英文标题】:Connecting dropdown picker to database 【发布时间】:2021-06-10 22:46:48 【问题描述】:我正在做一个项目,我必须创建一个注册页面,并且在该页面中我必须有一个下拉选择器,以便用户可以选择他们来自的国家/地区。我正在按照我在 youtube 上找到的这个视频中的一些步骤进行操作:https://www.youtube.com/watch?v=E_0XDyy4ef8 和关于 udemy 的课程,但他们没有介绍如何创建后端代码以将其连接到数据库的步骤。如果有人可以帮助我,那就太好了!
xaml-
<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");
Register.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)
【问题讨论】:
那么也许你需要另一个关于如何从 c# 查询数据库的视频 - 对于 SO 问题来说有点宽泛 【参考方案1】:您是否为 VM 使用了 BindingContext?看看this。作为建议,如果您使用 VM,请尝试将单击按钮的代码放入其中。
【讨论】:
以上是关于将下拉选择器连接到数据库的主要内容,如果未能解决你的问题,请参考以下文章
使用会话管理器连接到 EC2 上的 django 项目突然被阻止