每当我试图通过应用程序注册用户时,即使满足了所有要求,它也总是失败。我包括在清单中,但这并没有解决任何问题。还启用了电子邮件/密码登录方法。
我真的不知道该怎么办,因为这是我第一次使用xamarin,当涉及到xamarin表单与xamarin android时,似乎有更多的帮助。
public class registerActivity : AppCompatActivity
{
TextInputLayout fullNameText;
TextInputLayout phoneText;
TextInputLayout emailtext;
TextInputLayout passwordText;
Button registerButton;
FirebaseAuth firebaseAuth;
FirebaseDatabase userCredentials;
taskCompletetionListener taskCompletetionListener = new taskCompletetionListener();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.registerPage);
Typeface tf = Typeface.CreateFromAsset(Assets, "Tw_Cen_MT.TTF");
var signUpTxt = FindViewById<TextView>(Resource.Id.signUpTxt);
signUpTxt.SetTypeface(tf, TypefaceStyle.Bold);
var signUpLink = FindViewById<TextView>(Resource.Id.signInLink);
signUpLink.SetTypeface(tf, TypefaceStyle.Bold);
var alreadyMemberTxt = FindViewById<TextView>(Resource.Id.haveAccTxt);
signUpTxt.SetTypeface(tf, TypefaceStyle.Bold);
Button signUpBtn = (Button)FindViewById(Resource.Id.registerBtn);
signUpBtn.SetTypeface(tf, TypefaceStyle.Bold);
InitializedFirebase();
firebaseAuth = FirebaseAuth.Instance;
ConnectControl();
}
void InitializedFirebase()
{
var app = FirebaseApp.InitializeApp(this);
if (app == null)
{
var options = new FirebaseOptions.Builder()
.SetApplicationId("semoto-4b53b")
.SetApiKey("AIzaSyBiGjJQwDewsyWxviBjIimw7pQBHkbHkiA")
.SetDatabaseUrl("https://semoto-4b53b.firebaseio.com")
.SetStorageBucket("semoto-4b53b.appspot.com")
.Build();
app = FirebaseApp.InitializeApp(this, options);
userCredentials = FirebaseDatabase.GetInstance(app);
}
else
{
userCredentials = FirebaseDatabase.GetInstance(app);
}
}
void ConnectControl()
{
fullNameText = (TextInputLayout)FindViewById(Resource.Id.nameField);
emailtext = (TextInputLayout)FindViewById(Resource.Id.emailField);
phoneText = (TextInputLayout)FindViewById(Resource.Id.phoneNumField);
passwordText = (TextInputLayout)FindViewById(Resource.Id.passwordField);
registerButton = (Button)FindViewById(Resource.Id.registerBtn);
registerButton.Click += RegisterButton_Click;
}
private void RegisterButton_Click(object sender, EventArgs e)
{
string fullName, phoneNum, email, password;
fullName = fullNameText.EditText.Text;
phoneNum = phoneText.EditText.Text;
email = emailtext.EditText.Text;
password = passwordText.EditText.Text;
if(fullName.Length <= 3)
{
fullNameText.ErrorEnabled = true;
fullNameText.Error = "Please enter a valid name";
return;
}
else if (!email.Contains("@"))
{
emailtext.ErrorEnabled = true;
emailtext.Error = "Please enter a valid email";
return;
}
else if(phoneNum.Length < 9)
{
phoneText.ErrorEnabled = true;
phoneText.Error = "Please enter a valid phone number";
return;
}
else if (password.Length < 8)
{
passwordText.ErrorEnabled = true;
passwordText.Error = "Password should be at least 8 characters long";
return;
}
registerUser(fullName, phoneNum, email, password);
}
void registerUser(string name, string phone, string email, string password)
{
taskCompletetionListener.success += TaskCompletetionListener_success;
taskCompletetionListener.failure += TaskCompletetionListener_failure;
firebaseAuth.CreateUserWithEmailAndPassword(email, password)
.AddOnSuccessListener(this, taskCompletetionListener)
.AddOnFailureListener(this, taskCompletetionListener);
}
private void TaskCompletetionListener_failure(object sender, EventArgs e)
{
Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
alert.SetTitle("Registration Failed!");
alert.SetMessage("Ensure that you meet all of the requirements. ");
alert.SetNeutralButton("OK", delegate
{
alert.Dispose();
});
alert.Show();
}
//reishinishite kudasai
private void TaskCompletetionListener_success(object sender, EventArgs e)
{
Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
alert.SetTitle("Registration Successful");
alert.SetMessage("Welcome!");
alert.SetNeutralButton("OK", delegate
{
alert.Dispose();
});
alert.Show();
}
}
}
我为任务完成监听器上了一堂课
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Gms.Tasks;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace Semoto.eventListeners
{
public class taskCompletetionListener : Java.Lang.Object, IOnSuccessListener, IOnFailureListener
{
public event EventHandler success;
public event EventHandler failure;
public void OnFailure(Java.Lang.Exception e)
{
failure?.Invoke(this, new EventArgs());
}
public void OnSuccess(Java.Lang.Object result)
{
success?.Invoke(this, new EventArgs());
}
}
}
这不是一个答案,只是为了让您更容易进行故障排除。
第一步,你需要知道是什么导致了失败。
更改您的OnFailure
侦听器,如:
public void OnFailure(Java.Lang.Exception e)
{
failure?.Invoke(e, new EventArgs());
}
然后在您的活动中,打印错误消息,在这里使用对话框显示:
private void TaskCompletetionListener_failure(object sender, EventArgs e)
{
Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
alert.SetTitle("Registration Failed!");
alert.SetMessage(((Java.Lang.Exception)sender).Message);
alert.SetNeutralButton("OK", delegate
{
alert.Dispose();
});
alert.Show();
}
然后你可以显示你的错误信息,我们可以帮助你更快地找到解决方案。(附言:我用你的代码来测试我的账户,效果很好(。