Xamarin.Forms PopModalAsync 不起作用



我正在使用Xamarin构建一个应用程序。form和Xamarin。登录Facebook的权限

我是这样做的:

App.cs:

public App()
{
    if (IsAuthenticated)
    {
        MainPage = new NavigationPage(new DetailPage());
    }
    else {
        MainPage = new NavigationPage(new WelcomePage());
    }
}
public Action SuccessfulLoginAction
{
    get
    {
        return new Action(() => MainPage.Navigation.PopModalAsync());
    }
}

WelcomePage:

using System;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Project
{
    public class WelcomePage : ContentPage
    {
        public WelcomePage()
        {
            var login = new Button { Text = "Login" };
            login.Clicked += async (sender, e) =>
            {
                await Navigation.PushModalAsync(new LoginPage());
            };
            Content = new StackLayout
            {
                BackgroundColor = Color.FromHex("F0C640"),
                Padding = new Thickness(10, 40, 10, 10),
                Children = {
                    new Label { Text = "Welcome", FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.White },
                    new Label { Text = "Please login", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.White },
                    login
                }
            };
        }
    }
}

LoginPage:

using Xamarin.Forms;
using System.Threading.Tasks;
namespace Project
{
    public class LoginPage : ContentPage
    {
    }
}

LoginRenderer:

using System;
using Xamarin.Auth;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using Project;
using Project.iOS;
[assembly: ExportRenderer(typeof(LoginPage), typeof(LoginPageRenderer))]
namespace Project.iOS
{
    public class LoginPageRenderer : PageRenderer
    {
        bool IsShown;
        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);
            // Fixed the issue that on iOS 8, the modal wouldn't be popped.
            // url : http://stackoverflow.com/questions/24105390/how-to-login-to-facebook-in-xamarin-forms
            if (!IsShown)
            {
                IsShown = true;
                var auth = new OAuth2Authenticator(
                    clientId: App.Instance.OAuthSettings.ClientId, // your OAuth2 client id
                    scope: App.Instance.OAuthSettings.Scope, // The scopes for the particular API you're accessing. The format for this will vary by API.
                    authorizeUrl: new Uri(App.Instance.OAuthSettings.AuthorizeUrl), // the auth URL for the service
                    redirectUrl: new Uri(App.Instance.OAuthSettings.RedirectUrl)); // the redirect URL for the service
                auth.Completed += (sender, eventArgs) =>
                {
                    // We presented the UI, so it's up to us to dimiss it on iOS.
                    DismissViewController (true, null);
                    if (eventArgs.IsAuthenticated)
                    {
                        Console.WriteLine("Authenticated!!!!");
                        App.Instance.SaveToken(eventArgs.Account.Properties["access_token"]);
                        App.Instance.SuccessfulLoginAction.Invoke();
                    }
                    else {
                        Console.WriteLine("Not authenticated!!!!");
                        App.Instance.CanceledLoginAction.Invoke();
                    }
                };
                PresentViewController(auth.GetUI(), true, null);
            }
        }
    }
}

一切都工作得很好,我可以显示带有Facebook内容和登录的Modal页面。当身份验证完成后,它将通过验证。完成事件并调用App.Instance.SuccessfulLoginAction.Invoke();但是MainPage.Navigation.PopModalAsync()(在App.cs中)不会关闭Modal页面。

我猜的是Modal页面在欢迎页面中打开,我试图在App.cs文件中关闭它。有没有可能它没有指向同一个对象,所以我不能从这里关闭它?当你创建一个平台特定的渲染器时,你们是如何关闭你的模态页面的?

如何关闭这个模态页面?

谢谢

虽然不是很优雅,但在我的验证示例中,我是这样处理验证的:

 public void HandleLoginSucceeded(object sender, EventArgs e)
    {   
        MainPage = new WelcomePage();
    }

由于App.cs是入口点,我只是将MainPage重置为指向一个新位置。我不知道这可能会造成什么权衡;然而,我知道它是有效的。你可以在Github上看到我的示例,看看我的实现。

相关内容

  • 没有找到相关文章

最新更新