2017-02-23 44 views
0

我正在实施Facebook登录到Xamarin表单项目使用Xamarin.Auth plugin。这是ios渲染代码。Xamarin验证视图关闭问题

当我点击取消按钮或成功登录时,会调用dismissviewcontroller并关闭Web视图。

当我再次点击这个Facebook或登录按钮时,我得到的错误。

警告:试图在 上展示,其视图不在窗口层次结构中!

如果有人有一个很好的解决方案,请帮助我。

[assembly: ExportRenderer(typeof(FBLoginPage), typeof(FBLoginPageRenderer))] 
namespace vidmoji.iOS 
{ 
    public class FBLoginPageRenderer : PageRenderer 
    { 
     bool IsShown; 


     public override void ViewDidAppear(bool animated) 
     { 
      base.ViewDidAppear(animated); 

      if (!IsShown) 
      { 
       IsShown = true; 

       var auth = new OAuth2Authenticator(
        clientId: App.Instance.FBAuthSettings.ClientId, 
        clientSecret: App.Instance.FBAuthSettings.SecurityKey, 
        accessTokenUrl: new Uri(App.Instance.FBAuthSettings.AccessTokenUrl), 
        scope: App.Instance.FBAuthSettings.Scope, 
        authorizeUrl: new Uri(App.Instance.FBAuthSettings.AuthorizeUrl), 
        redirectUrl: new Uri(App.Instance.FBAuthSettings.RedirectUrl)); 



       auth.Completed += (sender, eventArgs) => 
       { 
        DismissViewController(true, null); 
        if (eventArgs.IsAuthenticated) 
        { 
         App.Instance.loginWithFacebook(eventArgs.Account.Properties["access_token"]); 
        } 
        else { 
         DismissModalViewController(true); 
        } 
       }; 

       PresentViewController(auth.GetUI(), true, null); 
      } 
     } 
    } 
} 
+0

非常感谢。 :) – Meteoric

回答

1

更新答案

在你FBLoginPage添加一个公共方法。

public partial class FBLoginPage : ContentPage 
{ 
    //...... 

    public void OnAuthenticationCompleted (bool isAuthenticated, string accessToken) 
    { 
     //Get your token and do what you need to do with it. 
     Navigation.PopModalAsync (true); 
    } 
} 

和您的渲染器类将其更改为:

public override void ViewDidAppear (bool animated) 
{ 
    base.ViewDidAppear (animated); 

    if (!IsShown) 
    { 
     IsShown = true; 

     var auth = new OAuth2Authenticator(
      clientId: App.Instance.FBAuthSettings.ClientId, 
      clientSecret: App.Instance.FBAuthSettings.SecurityKey, 
      accessTokenUrl: new Uri(App.Instance.FBAuthSettings.AccessTokenUrl), 
      scope: App.Instance.FBAuthSettings.Scope, 
      authorizeUrl: new Uri(App.Instance.FBAuthSettings.AuthorizeUrl), 
      redirectUrl: new Uri(App.Instance.FBAuthSettings.RedirectUrl)); 

     auth.Completed += (sender, eventArgs) => { 

      var element = Element as LoginPage; 

      var token = eventArgs.IsAuthenticated ? eventArgs.Account.Properties ["access_token"] : null; 

      element?.OnAuthenticationCompleted (eventArgs.IsAuthenticated, token); 
     }; 

     PresentViewController (auth.GetUI(), true, null); 
    } 

} 

关于更新。

此组件自2年前未更新,但nuget package为。我建议从组件更改为Nuget包。

祝你好运。

+0

感谢您的支持,我移动了代码,但它发生同样的错误。 当我点击Facebook按钮时,它运行良好,当我点击页面上的取消按钮并再次点击Facebook按钮时,会发生此错误。 – Meteoric

+0

私人无效OnFacebookButtonClicked(对象发件人,EventArgs的) \t \t { \t \t \t Navigation.PushModalAsync(新FBLoginPage()); \t \t} – Meteoric

+0

您可以尝试将库更新到nuget包并删除组件,因为您正在使用的组件已经有2年历史了。 – apineda