Как мне создать средство выбора, которое дает пользователю возможность выбирать свою роль пользователя (например, студент, преподавательский состав или безопасность) при создании своей учетной записи. Я создал флаг для каждой роли: student = 1, faculty = 2 и security = 3. Я хочу сохранить значение флага вместе со всей другой информацией, которую пользователь ввел в базу данных Firebase Realtime. Я создал средство выбора, но не знаю, как настроить его так, чтобы «когда эта роль была выбрана, добавьте этот флаг пользователю».
Вот мой код:
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");
}
}
}
}