从平台特定 (UWP) PageRenderer 导航并返回 PCL 页面

Navigating from Platform Specific (UWP) PageRenderer and back to PCL Page

我有两个问题:

  1. 在 UWP 中,如何导航到提供商登录 UI?更具体地说,iOS 和 Android 中以下代码片段在 UWP 中的等效项是什么?在iOS中,使用了如下代码: PresentViewController(auth.GetUI(), true, null); 其中 auth 是 Xamarin.Auth.OAuth2Authenticator 对象。 在 Android 中使用以下内容: activity.StartActivity(auth.GetUI(activity));

我正在寻找 UWP 中的等效代码。请记住,这些调用是从 LoginPage 进行的,而 LoginPage 派生自平台特定的 PageRenderer

  1. 如何在成功验证后导航回我的主页(位于 PCL)?

该代码基于以下来源的示例: http://www.c-sharpcorner.com/article/oauth-login-authenticating-with-identity-provider-in-xamarin-forms/

这是我的登录页面代码:

using System;
using Valufy;
using Xamarin.Forms.Platform.UWP;
using System.ComponentModel;
using Valufy.UWP;
using Valufy.AuthConfiguration;
using Xamarin.Forms;
[assembly: ExportRenderer(typeof(ProviderLoginPage), typeof(LoginRenderer))]

namespace Valufy.UWP
{
class LoginRenderer: PageRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.Page> e)
{
    base.OnElementChanged(e);

    //Get and Assign ProviderName from ProviderLoginPage
    ProviderLoginPage loginPage = (ProviderLoginPage)Element;
    string providername = loginPage.ProviderName;

        //Create OauthProviderSetting class with Oauth Implementation .Refer Step 6
        OAuthProviderSetting oauth = new OAuthProviderSetting();

            Xamarin.Auth.OAuth2Authenticator auth = oauth.LoginWithProvider(providername);  

            // After facebook,google and all identity provider login completed 
            auth.Completed += Auth_Completed;

            Type page_type = auth.GetUI();
//////THIS IS WHERE I AM STUCK...HOW DO I GO TO THE PROVIDER AUTH UI ////////////

//this.Frame.Navigate(page_type, auth);
//parentPage.Navigation.PushModalAsync(auth.GetUI());

            }

    }

    private void Auth_Completed(object sender, Xamarin.Auth.AuthenticatorCompletedEventArgs e)
    {
        if (e.IsAuthenticated)
        {
            OAuthConfig.User = new UserDetails();
            // Get and Save User Details 
            OAuthConfig.User.Token = e.Account.Properties["oauth_token"];
            OAuthConfig.User.TokenSecret = e.Account.Properties["oauth_token_secret"];
            OAuthConfig.User.TwitterId = e.Account.Properties["user_id"];
        OAuthConfig.User.ScreenName = e.Account.Properties["screen_name"];

/////NOW, HOW GO I GO BACK TO THE CALLING PAGE IN PCL ///////////////////

    }
    else
    {
        // The user cancelled
/////NOW, HOW GO I GO BACK TO THE CALLING PAGE IN PCL ///////////////////
}
}
}
}                

1- In UWP, how do I navigate to the provider login UI

您需要创建一个 UWP 页面对象,您将用于在渲染器中显示的同一对象就是您将用于进行导航的对象。

Type page_type = auth.GetUI();

page = new MyUWPLoginPage();

page.Frame.Navigate(page_type, auth);

2- How do I navigate back to my MainPage (which is in PCL) upon successful authentication?

有很多方法可以做到这一点,最简单的方法是在 ProviderLoginPage class 中创建一个 public 方法,然后从渲染器中 classes 调用此方法和 pass-in 参数。

public class ProviderLoginPage: ContentPage
{
    ......

    public void AuthenticationCompleted(object sender, Xamarin.Auth.AuthenticatorCompletedEventArgs e)
    {
        // Do your logic
    }

}

在您的渲染器中使用元素:

private void Auth_Completed(object sender, Xamarin.Auth.AuthenticatorCompletedEventArgs e)
{
    var xamElement = Element as ProviderLogin;

    xamElement?.AuthenticationCompleted(sender, e);
}

将所有逻辑移至 PCL class,这样您就不必在每个渲染器上重复它。

这应该有所帮助。

以下是导航到 UWP 提供商登录的代码: WindowsPage windowsPage = new WindowsPage();

                _frame = windowsPage.Frame;
                if (_frame == null)
                {
                    _frame = new Windows.UI.Xaml.Controls.Frame
                    {
                        Language = global::Windows.Globalization.ApplicationLanguages.Languages[0]
                    };
                    windowsPage.Content = _frame;
                    SetNativeControl(windowsPage);
                }

                Type pageType = auth.GetUI();
                _frame.Navigate(pageType, auth);

要在成功验证后导航回我的页面,代码如下:

private async void Auth_Completed(object sender, Xamarin.Auth.AuthenticatorCompletedEventArgs e)
    {
        if (e.IsAuthenticated)
        {

            var request = new OAuth2Request("GET", new Uri("https://login.microsoftonline.com/common/oauth2/V2.0/token?oauth2_access_token=" + e.Account.Properties["access_token"]), null, e.Account);
            try
            {
                string response = await MSGetUserInfo(e.Account);


            }
            catch (System.OperationCanceledException)
            {
                                }
            catch (Exception ex)
            {
                                }
            this.Element.Navigation.PushModalAsync(new MainPage());


        }
        else
        {
            // The user cancelled
        }
    }