【问题标题】:Xamarin auth view close issueXamarin auth 查看关闭问题
【发布时间】:2017-02-23 20:31:17
【问题描述】:

我正在使用 Xamarin.Auth plugin 实现对 Xamarin 表单项目的 facebook 登录。这是ios渲染代码。

当我点击取消按钮或登录成功时,会调用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);
            }
        }
    }
}

【问题讨论】:

  • 非常感谢。 :)

标签: ios xamarin.ios xamarin.forms


【解决方案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 包。

祝你好运。

【讨论】:

  • 感谢您的支持,我移动了代码,但出现同样的错误。当我第一次点击facebook按钮时,它运行良好,当我点击页面上的取消按钮并再次点击facebook按钮时,就会出现这个错误。
  • private void OnFacebookButtonClicked(object sender, EventArgs e) { Navigation.PushModalAsync(new FBLoginPage()); }
  • 您能否尝试将库更新为 nuget 包并删除该组件,因为您使用的组件已经 2 年了。
猜你喜欢
  • 1970-01-01
  • 2020-01-22
  • 2018-04-15
  • 2019-06-11
  • 2021-05-11
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多