2016-10-07 10 views
0

Facebookでログインするには、Xamarin.FormsとXamarin.Authを使用してアプリケーションを構築しています。ここで Xamarin.Forms PopModalAsyncが動作しない

は私がやっているものです:

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()が呼び出されます。 App.cs内のMainPage.Navigation.PopModalAsync()はモーダルページを閉じません。

私が推測しているのは、モーダルページがウェルカムページで開かれていて、それをApp.csファイルで閉じることです。同じオブジェクトを参照していない可能性があるので、ここから閉じることはできませんか?プラットフォーム固有のレンダラーを作成するときに、どのようにしてモーダルページを閉じますか?

このモーダルページを閉じるにはどうすればよいですか?ここ

おかげ

答えて

0

非常にエレガントではないが、私は、認証のための私のサンプルで認証を取り扱う方法です:

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

App.csは、エントリー・ポイントですので、私はちょうど新しいのポイントにメインページをリセットロケーション。私はこれが引き起こす可能性のあるトレードオフについて認識していません。しかし、私はそれが動作することを認識しています。あなたは私の実装を見るためにGithubに私のサンプルを見ることができます。

関連する問題