我正在使用Xamarin.Auth插件实现Xamarin的facebook登录。这是ios呈现代码。
当我单击“取消”按钮或成功登录时,会调用“拒绝”视图并关闭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);
}
}
}
}发布于 2017-02-24 00:02:17
更新应答
在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);
}
}关于最新情况。
这个组件从两年前就没有更新过,但是nuget包装已经更新了。我建议从组件改为Nuget包。
祝好运。
https://stackoverflow.com/questions/42425627
复制相似问题