Я видел NullReference, когда я нажимал на кнопку Войти или Зарегистрироваться, пожалуйста, помогите мне. Моё приложение Cra sh при входе / регистрации, с firebase. Я полностью следую официальному руководству Firebase.
Это код входа в систему .cs
namespace yourActivity
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
IFirebaseAuthenticator auth;
public MainPage()
{
InitializeComponent();
auth = DependencyService.Get<IFirebaseAuthenticator>();
}
async void LoginClicked(object sender, EventArgs e)
{
var token = await auth.LoginWithEmailPassword(EmailInput.Text, PasswordInput.Text);
if (token != "")
await Navigation.PushAsync(new TabbedPage1());
else
ShowError();
}
async void CreateAccountClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new CreateAccountPage());
}
private async void ShowError()
{
await DisplayAlert("Ops!", "E-mail o password errate. Ritenta o Registrati!", "OK");
}
}
}
Это код входа в систему .xaml
<ContentPage x:Class="yourActivity.MainPage">
<StackLayout VerticalOptions="Center Margin="20">
<Label
Text="yourActivity" HorizontalOptions="Center" TextColor="#4682B4"
FontSize="35"
Margin="0, 20"/>
<Entry
Placeholder="E-mail" Keyboard="Email" x:Name="EmailInput"/>
<Entry
Placeholder="Password"
IsPassword="true"
x:Name="PasswordInput"/>
<Button
Text="Login"
Clicked="LoginClicked"
Margin="60, 40"
BackgroundColor="#4682B4"
TextColor="White"/>
<Button
Text="Crea Account"
Clicked="CreateAccountClicked"
Margin="60, 40"
BackgroundColor="White"
FontSize="Small"
TextColor="#4682B4"/>
</StackLayout>
</ContentPage>
Я не знаю, как решить эту ошибку, это IfirebaseAuthenticator.cs
namespace yourActivity
{
public interface IFirebaseAuthenticator
{
Task<string> LoginWithEmailPassword(string email, string password);
string SignUpWithEmailPassword(string email, string password);
string GetUserEmail();
}
}
Это FireBaseAuthenticator.cs android
namespace yourActivity.Droid
{
class FirebaseAuthenticator : IFirebaseAuthenticator
{
public async Task<string> LoginWithEmailPassword(string email, string password)
{
try
{
var user = await
FirebaseAuth.Instance.SignInWithEmailAndPasswordAsync(email, password);
var token = await user.User.GetIdTokenAsync(false);
return token.Token;
}
catch (FirebaseAuthInvalidUserException e)
{
e.PrintStackTrace();
return "";
}
}
public string SignUpWithEmailPassword(string email, string password)
{
var signUpTask = FirebaseAuth.Instance.CreateUserWithEmailAndPassword(email, password);
return signUpTask.Exception?.Message;
}
public string GetUserEmail()
{
return FirebaseAuth.Instance.CurrentUser.Email;
}
}
}
CreateAccountPage.cs
namespace yourActivity
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CreateAccountPage : ContentPage
{
IFirebaseAuthenticator auth;
public CreateAccountPage()
{
InitializeComponent();
auth = DependencyService.Get<IFirebaseAuthenticator>();
}
async void SalveUserClicked(object sender, EventArgs e)
{
var token = auth.SignUpWithEmailPassword(EmailInput.Text, PasswordInput.Text);
if (token == null)
{
await Navigation.PushAsync(new MainPage());
ShowSuccess();
}
else
ShowError(token);
}
private async void ShowSuccess()
{
await DisplayAlert("Successo!", "Utente Creato con successo!", "OK");
}
private async void ShowError(string mensagem)
{
await DisplayAlert("Ops!", mensagem, "OK");
}
}
}