Xamarin.Forms PopModalAsync不起作用

时间:2016-10-07 03:41:29

标签: c# xamarin.ios modal-dialog xamarin.forms xamarin.auth

我正在使用Xamarin.Forms和Xamarin.Auth建立一个应用程序来登录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内容和登录的模态页面。身份验证完成后,它将通过auth.Completed事件并调用App.Instance.SuccessfulLoginAction.Invoke();但MainPage.Navigation.PopModalAsync()(在App.cs中)不会关闭模态页面。

我猜测的是Modal页面在欢迎页面中打开,我尝试在App.cs文件中关闭它。是否有可能它没有引用同一个对象,所以我无法从这里关闭它?在创建特定于平台的渲染器时,您如何关闭模态页面?

如何关闭此模态页?

由于

1 个答案:

答案 0 :(得分:0)

虽然不是很优雅,但我在样本中处理身份验证的方式如下:

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

由于App.cs是入口点,我只是将MainPage重置为指向新位置。我不知道这可能导致的权衡;但是,我知道它有效。您可以在Github上查看我的示例,以查看我的实施情况。