其他分享
首页 > 其他分享> > android-Auth0与Xamarin PCL依赖关系服务NullReferenceException

android-Auth0与Xamarin PCL依赖关系服务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
          ...
    {

标签:auth0,xamarin,xamarin-forms,xamarin-studio,android
来源: https://codeday.me/bug/20191028/1949873.html