Auth0 与 Xamarin PCL 依赖服务 NullReferenceException

Auth0 with Xamarin PCL Dependency Service NullReferenceException

我正在尝试将 DependencyService 与 Auth0 组件一起使用,但遇到了 System.NullReferenceException:对象引用未设置为对象实例错误。

以下是我在 Android 项目中的代码:

namespace LoginPattern.Android
{

[assembly: Xamarin.Forms.Dependency (typeof(LoginPattern.Android.Auth0WidgetLogin))]
public class Auth0WidgetLogin : FormsApplicationActivity, LoginPattern.IAuth0WidgetLogin
{

    private Auth0Client auth0 = new Auth0Client (
                                    "xxx.auth0.com",
                                    "xxxxxxxxxxxxxx");

    public Auth0WidgetLogin ()
    {
    }

    public async Task<LoginPattern.User> LoginUseAuth0EmbeddedWidget()
    {
        Auth0User usr = null;


        try {
            usr = await this.auth0.LoginAsync(Forms.Context);
        } catch (Exception ex) {
            throw ex;
        }

        LoginPattern.User userObj = new User(usr.Auth0AccessToken);

        return userObj;

    }
}
}

这在共享库中:

namespace LoginPattern
{
    public interface IAuth0WidgetLogin
    {
        Task<User> LoginUseAuth0EmbeddedWidget();
    }

    public class User
    {
        public User(
            string accessToken)
        {
            AccessToken = accessToken;
        }

        public string AccessToken {get; private set;}
        public string Scope {get; private set;}
    }
}

调用依赖服务时出现错误:

public async void Login ()
    {
        LoginPattern.User usr = null;
        usr = await DependencyService.Get<IAuth0WidgetLogin>().LoginUseAuth0EmbeddedWidget();
        App.Current.Properties["IsLoggedIn"] = true;
    }

将您的属性放在上方 命名空间声明:

    [assembly: Xamarin.Forms.Dependency (typeof(LoginPattern.Android.Auth0WidgetLogin))]
    namespace LoginPattern.Android
    {
          public class Auth0WidgetLogin : FormsApplicationActivity, LoginPattern.IAuth0WidgetLogin
          ...
    {