我正在尝试实现登录&使用Firebase和Xamarin进行注册认证。表单和我有以下错误:
- 在Android项目中,它在MainActivity.cs中抱怨这行(公共类FirebaseAuthenticator: IFirebaseAuthenticator)如下:(错误CS0246类型或名称空间名称'IFirebaseAuthenticator'找不到(您是否缺少using指令或程序集引用?)
- 在iOS项目中,它在AppDelegate.cs中抱怨这行(公共类FirebaseAuthenticator: IFirebaseAuthenticator)如下:(错误CS0246类型或命名空间名称'IFirebaseAuthenticator'无法找到(您是否缺少using指令或程序集引用?))
这是我的AppShell.cs,在这里我定义了共享代码:
using AbuseAlert.Views;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace AbuseAlert
{
public partial class AppShell : Xamarin.Forms.Shell
{
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute(nameof(ItemDetailPage), typeof(ItemDetailPage));
Routing.RegisterRoute(nameof(NewItemPage), typeof(NewItemPage));
}
private async void OnMenuItemClicked(object sender, EventArgs e)
{
await Shell.Current.GoToAsync("//LoginPage");
}
}
}
namespace AbuseAlert.Interfaces.FirebaseAuthentication
{
public interface IFirebaseAuthenticator
{
Task<string> SignInWithEmailAndPasswordAsync(string email, string password);
Task<string> SignUpWithEmailPasswordAsync(string email, string password);
Task ForgotPasswordAsync(string email);
}
}
这是Android项目中的MainActivity.cs,第一个错误发生的地方:
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using AndroidX.Core.App;
using AndroidX.Core.Content;
using MediaManager;
using Android;
using Octane.Xamarin.Forms.VideoPlayer.Android;
using LibVLCSharp.Forms.Shared;
using Android.Content;
using Firebase;
using Firebase.Auth;
using System.Threading.Tasks;
using Xamarin.Forms;
using AbuseAlert.Droid.Authentication.Firebase;
[assembly: Dependency(typeof(FirebaseAuthenticator))]
namespace AbuseAlert.Droid
{
[Activity(Label = "AbuseAlert", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public static FirebaseAuth Auth;
public event EventHandler<ActivityResultEventArgs> ActivityResult = delegate { };
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
CrossMediaManager.Current.Init(this);
LibVLCSharpFormsRenderer.Init();
InitFirebaseAuth(); //Create an instance of FirebaseAuth
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.RecordAudio) != Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.RecordAudio }, 1);
}
FormsVideoPlayer.Init();
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
ActivityResult(this, new ActivityResultEventArgs
{
RequestCode = requestCode,
ResultCode = resultCode,
Data = data
});
//base.OnActivityResult(requestCode, resultCode, data);
//AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data);
}
private void InitFirebaseAuth()
{
var options = new FirebaseOptions.Builder()
.SetProjectId("abusealert-53b22") //YOUR_PROJECT_ID")
.SetApplicationId("1:883750321469:android:65f90ffae4042a695b1954") //YOUR_APPLICATION_ID")
.SetApiKey("AIzaSyBk_4DWo-bMvrqOI2FfsfuK61HEF-OY3J8") //YOUR_FIREBASE_API_KEY")
.SetDatabaseUrl("https://abusealert-53b22-default-rtdb.firebaseio.com") //YOUR_DATABASEURL")
.SetStorageBucket("nam5 (us-central)") //YOUR_STORAGEBUCKET")
.Build();
//https://console.firebase.google.com/u/0/project/abusealert-53b22/database/abusealert-53b22-default-rtdb/data/~2F
var _fireApp = FirebaseApp.InitializeApp(this, options);
Auth = FirebaseAuth.GetInstance(_fireApp);
}
}
}
namespace AbuseAlert.Droid.Authentication.Firebase
{
public class FirebaseAuthenticator : IFirebaseAuthenticator
{
public async Task<string> LoginWithEmailPasswordAsync(string email, string password)
{
var user = await MainActivity.Auth.SignInWithEmailAndPasswordAsync(email, password);
var token = await user.User.GetIdTokenAsync(false);
return token.Token;
}
public async Task<string> SignUpWithEmailPasswordAsync(string email, string password)
{
var user = await MainActivity.Auth.CreateUserWithEmailAndPasswordAsync(email, password);
var token = await user.User.GetIdTokenAsync(false);
return token.Token;
}
public async Task ConfirmPasswordResetAsync(string code, string newPassword)
{
await MainActivity.Auth.ConfirmPasswordResetAsync(code, newPassword);
}
public async Task ForgotPasswordAsync(string email)
{
await MainActivity.Auth.SendPasswordResetEmailAsync(email);
}
public async Task VerifyPasswordResetCodeAsync(string code)
{
await MainActivity.Auth.VerifyPasswordResetCodeAsync(code);
}
}
}
这是发生第二个错误的iOS项目的AppDelegate.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AbuseAlert.iOS.Implementations.Auth;
using Foundation;
using MediaManager;
using Octane.Xamarin.Forms.VideoPlayer.iOS;
using UIKit;
using Xamarin.Forms;
[assembly: Dependency(typeof(FirebaseAuthenticator))]
namespace AbuseAlert.iOS
{
// The UIApplicationDelegate for the application. This class is responsible for
launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
CrossMediaManager.Current.Init();
FormsVideoPlayer.Init();
Firebase.Core.App.Configure();
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
}
namespace AbuseAlert.iOS.Implementations.Auth
{
class FirebaseAuthenticator : IFirebaseAuthenticator
{
public async Task<string> LoginWithEmailPassword(string email, string password)
{
var authDataResult = await Firebase.Auth.Auth.DefaultInstance.SignInWithPasswordAsync(
email,
password);
return await authDataResult.User.GetIdTokenAsync();
}
public async Task<string> SignupWithEmailPassword(string email, string password)
{
var authDataResult = await Firebase.Auth.Auth.DefaultInstance.CreateUserAsync(
email,
password);
return await authDataResult.User.GetIdTokenAsync();
}
}
}
那么我需要做些什么来清除这些错误呢?
这是一个非常基本的c#错误,在文档
中有详细讨论。您已经在AbuseAlert.Interfaces.FirebaseAuthentication
名称空间中定义了IFirebaseAuthenticator
namespace AbuseAlert.Interfaces.FirebaseAuthentication
{
public interface IFirebaseAuthenticator
所以当你想在代码的其他地方使用IFirebaseAuthenticator
时,你需要告诉它使用哪个namespace
using AbuseAlert.Interfaces.FirebaseAuthentication;
或者在使用IFirebaseAuthenticator
AbuseAlert.Interfaces.FirebaseAuthentication.IFirebaseAuthenticator
在很多情况下,如果你在VS中右键单击错误,它会提供建议的修复,你可以从
中选择