【问题标题】:Xamarin.Auth Facebook gives NullReferenceException 2Xamarin.Auth Facebook 给出 NullReferenceException 2
【发布时间】:2018-11-06 07:44:04
【问题描述】:

我正在关注This Page 以及来自 Xamarin.Auth github 存储库的一些示例,因此请设置一个 facebook 登录。我创建了以下登录功能:

public void Authenticate()
{
    string clientId = Config.FacebookAppID;
    string redirectUri = redirectUri = "MyApp:/authorize";
    var authenticator = new OAuth2Authenticator(
        clientId: clientId,
        scope: "public_profile,email",
        authorizeUrl: new Uri("https://www.facebook.com/v2.9/dialog/oauth"),
        redirectUrl: new Uri(redirectUri),
        getUsernameAsync: null,
        isUsingNativeUI: true);

    authenticator.AllowCancel = true;
    authenticator.Completed += OnAuthCompleted;
    authenticator.Error += OnAuthError;
    Authenticator = authenticator;

    var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
    presenter.Login(Authenticator);
}

但我在最后一行收到以下异常:

{System.NullReferenceException: Object reference not set to an instance of an object.
  at Xamarin.Auth.Presenters.OAuthLoginPresenter.Login (Xamarin.Auth.Authenticator authenticator) [0x00011] in C:\cxa\source\Xamarin.Auth.LinkSource\Request.cs:290 
  at MyApp.Components.AuthorisationManager.Authenticate () [0x00078] in D:\Dev\MenuSystem.AdminApp\MyApp\MyApp\MyApp\Components\AuthorisationManager.cs:56 
  at MyApp.ViewModels.MainPageViewModel.NavigateToMenuItem () [0x00008] in D:\Dev\MenuSystem.AdminApp\MyApp\MyApp\MyApp\ViewModels\MainPageViewModel.cs:63 }

任何想法我可能做错了什么?

【问题讨论】:

    标签: xamarin xamarin.auth


    【解决方案1】:

    我知道这已经有一年多了,但是,我在接受指导的链接培训后遇到了同样的问题。

    答案是在AppDelegate类的FinishedLaunching方法中初始化AuthenticationConfiguration

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        // Override point for customization after application launch.
        // If not required for your application you can safely delete this method
        // Add the Init() method here
        global::Xamarin.Auth.Presenters.XamarinIOS.AuthenticationConfiguration.Init();
        return true;
    }
    

    【讨论】:

    • 谢谢杰克的回答。我已经停止研究 xamarin ;),但惊讶于问题仍然存在!我想知道为什么我们正在遵循的教程没有遇到这个问题。不过,我已将其标记为答案,因为它肯定会帮助其他人。
    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 2011-02-04
    • 2016-11-04
    • 2017-02-07
    • 2019-05-02
    • 2020-07-13
    • 2020-12-14
    • 1970-01-01
    相关资源
    最近更新 更多