2

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;
    }
}   
Shahabuddin Vansiwala
  • 673
  • 1
  • 13
  • 24

1 Answers1

1

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.

Community
  • 1
  • 1
Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
  • I know! Could you please show your `App` code? In the code you have posted you do `App.SaveToken(eventArgs.Account.Properties["access_token"]);` so you save the token value to a variable in your app class. Probably there is a static property in you `App` class as well, with which you can access the token in any page or any place in your application – Gerald Versluis Dec 18 '15 at 10:02
  • Like I said, there is a static property Token in your App class. So when you have set the token, you can access it anywhere by `App.Token` – Gerald Versluis Dec 18 '15 at 10:20
  • Exactly. you are right... but I can't show token in label. please see updated post for profilepage.cs file code. – Shahabuddin Vansiwala Dec 18 '15 at 11:34
  • That is because your `Page` is created before the `Token` is being filled. So you should create a mechanism to update the value in you UI when it is set. I will update my answer. – Gerald Versluis Dec 18 '15 at 12:12