C# 具有firebase用户注册错误的Xamarin表单
我正在使用xamarin表单和firebase身份验证开发一个应用程序 使用xamarin.firebase.auth和xamarin.firebase.core 当我想创建一个新用户时,代码运行良好,但它给了我一个例外 Java.Lang.IllegalStateException:“任务尚未完成” 当我一行一行地跟踪代码时,每件事都很正常,我没有收到任何错误,但是当创建用户后运行应用程序时,它会给我一个异常 这是我的代码: pcl中的面:C# 具有firebase用户注册错误的Xamarin表单,c#,android,firebase,xamarin,firebase-authentication,C#,Android,Firebase,Xamarin,Firebase Authentication,我正在使用xamarin表单和firebase身份验证开发一个应用程序 使用xamarin.firebase.auth和xamarin.firebase.core 当我想创建一个新用户时,代码运行良好,但它给了我一个例外 Java.Lang.IllegalStateException:“任务尚未完成” 当我一行一行地跟踪代码时,每件事都很正常,我没有收到任何错误,但是当创建用户后运行应用程序时,它会给我一个异常 这是我的代码: pcl中的面: using System; using System
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace XamarinFirebaseAuth
{
public interface IAuth
{
Task<string> LoginWithEmailPassword(string email, string password);
bool SignUpWithEmailPassword(string email, string password);
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Threading.Tasks;
命名空间XamarinFirebaseAuth
{
公共接口IAuth
{
使用EmailPassword(字符串电子邮件、字符串密码)的任务登录;
bool SignUpWithEmailPassword(字符串电子邮件,字符串密码);
}
}
android实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foundation;
using UIKit;
using XamarinFirebaseAuth;
using XamarinFirebaseAuth.iOS;
using Firebase.Auth;
using System.Threading.Tasks;
using Xamarin.Forms;
[assembly: Dependency(typeof(AuthIOS))]
namespace XamarinFirebaseAuth.iOS
{
public class AuthIOS : IAuth
{
public async Task<string> LoginWithEmailPassword(string email, string password)
{
try
{
var user = await Auth.DefaultInstance.SignInWithPasswordAsync(email, password);
var token = user.User.GetIdTokenAsync();
return token.ToString();
}
catch(Exception e)
{
return "";
}
}
public bool SignUpWithEmailPassword(string email, string password)
{
try
{
var signUpTask = Auth.DefaultInstance.CreateUserAsync(email, password);
return true;
}
catch (Exception e)
{
throw;
//return false;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用基础;
使用UIKit;
使用XamarinFirebaseAuth;
使用XamarinFirebaseAuth.iOS;
使用Firebase.Auth;
使用System.Threading.Tasks;
使用Xamarin.Forms;
[程序集:依赖项(typeof(AuthIOS))]
命名空间XamarinFirebaseAuth.iOS
{
公共类AuthIOS:IAuth
{
使用EmailPassword的公共异步任务登录(字符串电子邮件、字符串密码)
{
尝试
{
var user=wait Auth.DefaultInstance.SignInWithPasswordAsync(电子邮件,密码);
var token=user.user.GetIdTokenAsync();
返回token.ToString();
}
捕获(例外e)
{
返回“”;
}
}
public bool SignUpWithEmailPassword(字符串电子邮件,字符串密码)
{
尝试
{
var signUpTask=Auth.DefaultInstance.CreateUserAsync(电子邮件,密码);
返回true;
}
捕获(例外e)
{
投掷;
//返回false;
}
}
}
}
这是我的注册页面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace XamarinFirebaseAuth
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SignUpPage : ContentPage
{
IAuth auth;
public SignUpPage()
{
InitializeComponent();
auth = DependencyService.Get<IAuth>();
}
private async void btnRegister_Clicked(object sender, EventArgs e)
{
try
{
bool created = auth.SignUpWithEmailPassword(EmailInput.Text, PasswordInput.Text);
if (created)
{
await DisplayAlert("Success", "Your account created successfully", "OK");
await Navigation.PopAsync();
}
else
{
await DisplayAlert("Error", "Something went wrong. Try again later!", "OK");
}
}
catch
{
throw;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Xamarin.Forms;
使用Xamarin.Forms.Xaml;
命名空间XamarinFirebaseAuth
{
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类SignUpPage:ContentPage
{
IAuth-auth;
公共签名页()
{
初始化组件();
auth=DependencyService.Get();
}
已单击私有异步无效BTN注册表(对象发送方,事件参数e)
{
尝试
{
bool created=auth.SignUpWithEmailPassword(EmailInput.Text,PasswordInput.Text);
如果(已创建)
{
等待DisplayAlert(“成功”、“您的帐户创建成功”、“确定”);
等待导航。PopAsync();
}
其他的
{
等待DisplayAlert(“错误”,“出现问题。请稍后再试!”,“确定”);
}
}
抓住
{
投掷;
}
}
}
}
我认为您应该等待CreateUserAsync
方法来了解帐户是否成功创建:
AuthDataResult signUpTask = await Auth.DefaultInstance.CreateUserAsync(email, password);
然后您可以获得用户信息:
await signUpTask.User.GetIdTokenAsync();