Shahabuddin Vansiwala
Shahabuddin Vansiwala

Reputation: 673

How to pass string from android render page to PCL page?

How to pass string value to android render page to PCL page.

I want to send token of eventArgs.Account.Properties["access_token"] to PCL page.

how can i do ?Please help.

[assembly: ExportRenderer(typeof(LoginPage), typeof(LoginRender))]
namespace TestApp.Droid.Renderers
{
    public class LoginRender : PageRenderer
    {
    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            // this is a ViewGroup - so should be able to load an AXML file and FindView<>
            var activity = this.Context as Activity;

            var auth = new OAuth2Authenticator(
                clientId: "", // your OAuth2 client id
                scope: "user_about_me", // the scopes for the particular API you're accessing, delimited by "+" symbols
                authorizeUrl: new Uri("https://www.facebook.com/dialog/oauth"), // the auth URL for the service
                redirectUrl: new Uri("https://www.facebook.com/connect/login_success.html")); // the redirect URL for the service

            auth.Completed += (sender, eventArgs) => {
                if (eventArgs.IsAuthenticated)
                {
                    Toast.MakeText(this.Context, eventArgs.Account.Properties["access_token"], ToastLength.Long).Show();

                    App.SuccessfulLoginAction.Invoke();

                    App.SaveToken(eventArgs.Account.Properties["access_token"]);

                }
                else
                {
                    // The user cancelled
                }
            };

            activity.StartActivity(auth.GetUI(activity));
        }
    }
}

App.cs

public class App
    {
        static NavigationPage _NavPage;

        public static Page GetMainPage()
        {
            var profilePage = new ProfilePage();

            _NavPage = new NavigationPage(profilePage);

            return _NavPage;
        }

        public static bool IsLoggedIn
        {
            get { return !string.IsNullOrWhiteSpace(_Token); }
        }

        static string _Token;
        public static string Token
        {
            get { return _Token; }
        }

        public static void SaveToken(string token)
        {
            _Token = token;
        }

        public static Action SuccessfulLoginAction
        {
            get
            {
                return new Action(() => {
                    _NavPage.Navigation.PopModalAsync();
                });
            }
        }
    }

above is my App.cs file code. static method can't return token.

ProfilePage.cs in PCL

public class ProfilePage : BaseContentPage
{
    public ProfilePage()
    {
        string tk = App.Token;

        var lbltoken = new Label()
        {
            FontSize = 20,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Text = tk,
        };
        var stack = new StackLayout
        {
            VerticalOptions = LayoutOptions.StartAndExpand,
            Children = { lbltoken },
        };
        Content = stack;
    }
}   

Upvotes: 2

Views: 691

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 34128

I'm presuming that you have followed this example here: How to login to facebook in Xamarin.Forms

In that case you can use it in your PCL by calling App.Token

If that isn't working create a static property of the field you are using by calling App.SaveToken(eventArgs.Account.Properties["access_token"]);

With the edits you have made it is apparent that you set the value of your Label before the App.Token has a value.

A quick fix here could be to hook in to the Page.Appearing event, like so;

public class ProfilePage : BaseContentPage
{
    private Label _lbltoken;

    public ProfilePage()
    {
        Appearing += (object s, EventArgs a) => {
           _lbltoken.Text = App.Token;
        };

        string tk = App.Token;

        _lbltoken = new Label()
        {
            FontSize = 20,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Text = tk,
        };
        var stack = new StackLayout
        {
            VerticalOptions = LayoutOptions.StartAndExpand,
            Children = { _lbltoken },
        };
        Content = stack;
    }
}

I've made your Label control a private variable so we can easily refer to it from elsewhere, and create a Event-handler for when your ProfilePage appears. So every time you Page appears, it will set the value of App.Token in your Label.

This should work. However you would probably be better of checking out techniques like MVVM.

Upvotes: 1

Related Questions