我如何创建一个选择器,使用户在创建帐户时可以选择其用户角色(即学生,教师或安全人员)。我为每个角色创建了一个标志,学生= 1,教师= 2,安全= 3。我想将标志值与用户输入到Firebase实时数据库中的所有其他信息一起存储。我已经创建了选择器,但不知道如何设置它,以便“在选择此角色时,然后将此标志添加到用户”。
这是我的代码:
FirebaseHelper.cs
//Insert a user
public static async Task<bool> AddUser(string email, string password, string studentid,
string firstname, string lastname, string carmake,
string carmodel, string caryear, string carcolor,
string licensenumber)
{
try
{
await firebase
.Child("Users")
.PostAsync(new Users()
{
Email = email,
Password = password,
StudentID = studentid,
FirstName = firstname,
LastName = lastname,
CarMake = carmake,
CarModel = carmodel,
CarYear = caryear,
CarColor = carcolor,
LicenseNumber = licensenumber,
SetAccountType = AccountType.student,
SetAccountStatus = AccountStatus.unlcoked,
NumberOfCitations = 0
});
return true;
}
catch (Exception e)
{
Debug.WriteLine($"Error:{e}");
return false;
}
}
SignUpPage.xaml(我有选择器)
<Label Text="Select your Role" FontSize="Default" Grid.Row="0" TextColor="Black" />
<Picker x:Name="picker"
Title="Select a role"
TitleColor="Black">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Student</x:String>
<x:String>Faculty</x:String>
<x:String>Security</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
<Button x:Name="signup" Text="Sign Up" BackgroundColor="#0077be" TextColor="White" Command="{Binding SignUpCommand}" IsEnabled="{Binding SignEnable}" HorizontalOptions="Center"/>
SignUpPage.xaml.cs
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SignUpPage : ContentPage
{
SignUpVM signUpVM;
public SignUpPage()
{
InitializeComponent();
signUpVM = new SignUpVM();
//set binding
BindingContext = signUpVM;
}
}
SignUpVM.cs
public class SignUpVM : INotifyPropertyChanged
{
private bool signenable = true;
public bool SignEnable
{
get { return signenable; }
set
{
signenable = value;
PropertyChanged(this, new PropertyChangedEventArgs("SignEnable"));
}
}
private bool truefalse = false;
public bool Truefalse
{
get { return truefalse; }
set
{
truefalse = value;
PropertyChanged(this, new PropertyChangedEventArgs("Truefalse"));
}
}
private string email;
public string Email
{
get { return email; }
set
{
email = value;
PropertyChanged(this, new PropertyChangedEventArgs("Email"));
}
}
private string studentid;
public string StudentID
{
get { return studentid; }
set
{
studentid = value;
PropertyChanged(this, new PropertyChangedEventArgs("StudentID"));
}
}
private string firstname;
public string FirstName
{
get { return firstname; }
set
{
firstname = value;
PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
}
}
private string lastname;
public string LastName
{
get { return lastname; }
set
{
lastname = value;
PropertyChanged(this, new PropertyChangedEventArgs("LastName"));
}
}
private string password;
public event PropertyChangedEventHandler PropertyChanged;
public string Password
{
get { return password; }
set
{
password = value;
PropertyChanged(this, new PropertyChangedEventArgs("Password"));
}
}
private string confirmpassword;
public string ConfirmPassword
{
get { return confirmpassword; }
set
{
confirmpassword = value;
PropertyChanged(this, new PropertyChangedEventArgs("ConfirmPassword"));
}
}
public Command SignUpCommand
{
get
{
return new Command(async () =>
{
// Read email address from Firebase database
var user = await FirebaseHelper.GetUser(Email);
// Check for symbols
var hasSymbols = new Regex(@"[!@#$%^&*()_+=\[{\]};:<>|./?,-]");
Truefalse = true;
SignEnable = false;
//null or empty field validation, check weather email and password is null or empty
if (string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(Password))
{
Truefalse = false;
SignEnable = true;
await App.Current.MainPage.DisplayAlert("Empty Values", "Please enter Email and Password", "OK");
}
else if (!Email.Contains("@"))
{
Truefalse = false;
SignEnable = true;
await App.Current.MainPage.DisplayAlert("", "Email Address is Invalid", "OK");
}
else if (Password.Length < 8)
{
Truefalse = false;
SignEnable = true;
await App.Current.MainPage.DisplayAlert("", "Password is less than 8 characters", "OK");
}
else if (!Password.Any(char.IsUpper))
{
Truefalse = false;
SignEnable = true;
await App.Current.MainPage.DisplayAlert("", "Password Must Contain at Least 1 Uppcase Letter", "OK");
}
else if (!hasSymbols.IsMatch(Password))
{
Truefalse = false;
SignEnable = true;
await App.Current.MainPage.DisplayAlert("", "Password should contain At least one special case characters", "OK");
}
else if (Password != ConfirmPassword)
{
Truefalse = false;
SignEnable = true;
await App.Current.MainPage.DisplayAlert("", "Password must be same as above!", "OK");
}
else if (string.IsNullOrEmpty(StudentID))
{
Truefalse = false;
SignEnable = true;
await App.Current.MainPage.DisplayAlert("Empty Values", "Please enter Student ID", "OK");
}
else if (StudentID.Length < 9)
{
Truefalse = false;
SignEnable = true;
await App.Current.MainPage.DisplayAlert("", "Student ID is Incorrect!", "OK");
}
else if (user != null)
{
if (Email == user.Email)
{
Truefalse = false;
SignEnable = true;
await App.Current.MainPage.DisplayAlert("Email Taken", "Please Use A Different Email", "Ok");
}
}
else
SignUp();
});
}
}
private async void SignUp()
{
//call AddUser function which we define in Firebase helper class
var user = await FirebaseHelper.AddUser(Email, Password, StudentID, FirstName, LastName, CarMake, CarModel,
CarYear, CarColor, LicenseNumber);
//AddUser return true if data insert successfuly
if (user)
{
await App.Current.MainPage.DisplayAlert("SignUp Success", "", "Ok");
//Navigate to Wellcom page after successfuly SignUp
//pass user email to welcom page
SignEnable = true;
Truefalse = false;
await App.Current.MainPage.Navigation.PushAsync(new LoginPage());
}
else
{
SignEnable = true;
Truefalse = false;
await App.Current.MainPage.DisplayAlert("Error", "SignUp Fail", "OK");
}
}
}
}
仅检查Picker
的值并适当设置AccountType
在后面的代码中,获取选择器的值
// default to student
var acctType = AccountType.Student;
if (picker.SelectedItem.ToString() == "Faculty") acctType = AccountType.Faculty;
if (picker.SelectedItem.ToString() == "Security") acctType = AccountType.Security;
然后将其通过您的VM传递到数据库层
await firebase
.Child("Users")
.PostAsync(new Users()
{
...
SetAccountType = acctType,
...
});