当我使用Firebase登录/注册时,崩溃应用程序xamarin.forms

问题描述 投票:0回答:1

单击登录或注册后,我看到了一个N​​ullReference,请帮助我使用Firebase登录/注册时,我的应用程序崩溃。我完全遵守Firebase的官方指南。

这是login.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");
        }
    }
}

这是login.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();
    }
}

这是Android上的FireBaseAuthenticator.cs

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");
        }
    } 
}
firebase authentication xamarin.forms sign
1个回答
0
投票

在调用对象的函数或属性之前检查Null引用。

© www.soinside.com 2019 - 2024. All rights reserved.