我对使用Xamarin.Forms并不陌生,我目前正在创建一个注册页面。我正在尝试创建一种方法来检查在创建帐户时是否已经使用了电子邮件。我正在使用Firebase实时数据库。我设法让应用程序检查电子邮件是否已使用并显示错误,但是当电子邮件有效(未使用)时,当我去创建帐户时,该应用程序不会执行任何操作。我想念什么?
我提供了代码
public Command SignUpCommand
{
get
{
return new Command(() =>
{
var hasSymbols = new Regex(@"[!@#$%^&*()_+=\[{\]};:<>|./?,-]");
//null or empty field validation, check weather email and password is null or empty
if (string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(Password))
{
App.Current.MainPage.DisplayAlert("Empty Values", "Please enter Email and Password", "OK");
}
else if (!Email.Contains("@"))
{
App.Current.MainPage.DisplayAlert("", "Email Address is Invalid", "OK");
}
else if (Password.Length < 8)
{
App.Current.MainPage.DisplayAlert("", "Password is less than 8 characters", "OK");
}
else if (!Password.Any(char.IsUpper))
{
App.Current.MainPage.DisplayAlert("", "Password Must Contain at Least 1 Uppcase Letter", "OK");
}
else if (!hasSymbols.IsMatch(Password))
{
App.Current.MainPage.DisplayAlert("", "Password should contain At least one special case characters", "OK");
}
else if (Password != ConfirmPassword)
{
App.Current.MainPage.DisplayAlert("", "Password must be same as above!", "OK");
}
else if (string.IsNullOrEmpty(StudentID))
{
App.Current.MainPage.DisplayAlert("Empty Values", "Please enter Student ID", "OK");
}
else if (StudentID.Length < 9)
{
App.Current.MainPage.DisplayAlert("", "Student ID is Incorrect!", "OK");
}
else
{
EmailCheck();
}
});
}
}
private async void EmailCheck()
{
//call GetUser function which we define in Firebase helper class
var user = await FirebaseHelper.GetUser(Email);
//firebase return null valuse if user data not found in database
if (user != null)
if (Email == user.Email)
{
await App.Current.MainPage.DisplayAlert("Email Already Used", "", "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
await App.Current.MainPage.Navigation.PushAsync(new LoginPage());
}
else
await App.Current.MainPage.DisplayAlert("Error", "SignUp Fail", "OK");
}
}
}
根据您的描述,您要创建项目,使用Eamil帐户注册帐户,并注册以浏览另一个内容页面。
但是我发现您的代码有点混乱,我做了一个简单的示例,您可以看一下:
<StackLayout>
<!-- Place new controls here -->
<Entry x:Name="txtemail" Placeholder="Email" />
<Entry x:Name="txtpassword" Placeholder="Enter Password" />
<Entry x:Name="txtstudentId" Placeholder="Enter Student Id" />
<StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
<Button
x:Name="btnSign"
Clicked="BtnSign_Clicked"
Text="Sign up"
WidthRequest="200" />
<Button
x:Name="btnRegister"
Clicked="BtnRegister_Clicked"
Text="Retrive"
WidthRequest="200" />
</StackLayout>
</StackLayout>
public partial class MainPage : ContentPage
{
FirebaseHelper firebaseHelper = new FirebaseHelper();
public MainPage()
{
InitializeComponent();
}
private async void BtnSign_Clicked(object sender, EventArgs e)
{
var person = await firebaseHelper.GetPerson(txtemail.Text);
if (person != null)
{
if(person.Email==txtpassword.Text)
{
await Navigation.PushAsync(new Page1());
}
else
{
await DisplayAlert("Fail", "Password is not correct!", "OK");
}
}
else
{
await DisplayAlert("Fail", "No email Available!", "OK");
}
}
private async void BtnRegister_Clicked(object sender, EventArgs e)
{
if(await RegisterAccount())
{
await firebaseHelper.AddPerson(txtemail.Text, txtpassword.Text, txtstudentId.Text);
txtemail.Text = string.Empty;
txtpassword.Text = string.Empty;
txtstudentId.Text = string.Empty;
await DisplayAlert("Success", "Person Added Successfully", "OK");
}
else
{
await DisplayAlert("Fail", "Person Added Fail", "OK");
}
}
private async Task<bool> RegisterAccount()
{
var hasSymbols = new Regex(@"[!@#$%^&*()_+=\[{\]};:<>|./?,-]");
//null or empty field validation, check weather email and password is null or empty
if (string.IsNullOrEmpty(txtemail.Text) || string.IsNullOrEmpty(txtpassword.Text))
{
await DisplayAlert("Empty Values", "Please enter Email and Password", "OK");
return false;
}
else if (!txtemail.Text.Contains("@"))
{
await DisplayAlert("", "Email Address is Invalid", "OK");
return false;
}
else if (txtpassword.Text.Length < 8)
{
await DisplayAlert("", "Password is less than 8 characters", "OK");
return false;
}
else if (!txtpassword.Text.Any(char.IsUpper))
{
await DisplayAlert("", "Password Must Contain at Least 1 Uppcase Letter", "OK");
return false;
}
else if (!hasSymbols.IsMatch(txtpassword.Text))
{
await DisplayAlert("", "Password should contain At least one special case characters", "OK");
return false;
}
else if (string.IsNullOrEmpty(txtstudentId.Text))
{
await DisplayAlert("Empty Values", "Please enter Student ID", "OK");
return false;
}
else
{
var person = await firebaseHelper.GetPerson(txtemail.Text);
if (person != null)
{
await DisplayAlert("fail", "Email already exist", "OK");
return false;
}
}
return true;
}
}
public class Register
{
public string Email { get; set; }
public string Password { get; set; }
public string StudentId { get; set; }
}
public class FirebaseHelper
{
FirebaseClient firebase = new FirebaseClient("https://testapp1-ebb27-6e5ba.firebaseio.com/");
public async Task<List<Register>> GetAllPersons()
{
return (await firebase
.Child("Persons")
.OnceAsync<Register>()).Select(item => new Register
{
Email = item.Object.Email,
Password = item.Object.Password,
StudentId=item.Object.StudentId
}).ToList();
}
public async Task AddPerson(string email, string password, string studentid)
{
await firebase
.Child("Persons")
.PostAsync(new Register() { Email = email, Password = password, StudentId = studentid });
}
public async Task<Register> GetPerson(string email)
{
var allPersons = await GetAllPersons();
await firebase
.Child("Persons")
.OnceAsync<Register>();
return allPersons.Where(a => a.Email == email).FirstOrDefault();
}
}
这是我在github上的示例: