MasterPage subPages with non-default constructor

TommyRczTommyRcz Member ✭✭

Hello guys, I'm now a bit used with Xamarin Forms, I created some apps, but I'm actually facing something that is blocking me:

I have to make an app with a MasterPage to access to other pages.

But my problem is that I need to pass an accessToken in parameter to the "subPages" to get them working, and TargetType / System.Type don't permit that kind of thing, neither use a non-default constructor that can take arguments/parameters.

Am I missing something ?

Is there something that could do it ?

Thanks in advance.

Best Answers

  • TommyRczTommyRcz ✭✭
    Accepted Answer

    Thanks a lot ! Putting my accessToken object in App Class solved my problemI don't know why I had not thought of that earlier, I was diving into darkness ;)

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    But my problem is that I need to pass an accessToken in parameter to the "subPages" to get them working

    Stop. Why do you have to pass it? Its the same token used through-out the app, right? Just put it at app level so its visible everywhere.

  • TommyRczTommyRcz Member ✭✭

    The Token is generated after the connexion page that is actually the first, is there a way to make it static then ?

    Thanks for your help

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The Token is generated after the connexion page that is actually the first

    Ok. And? You still put the Token object in the App class so all the other classes can reach it. Not seeing the problem.

    is there a way to make it static then

    Well. Yeah. Set it to static. What's that going to do for you though?

    Wait... thinking... Page constructors... static... Are you doing all this work in the Page code behind? Does this token live in the Page instead of in a ViewModel? And that's why you want it static so its the same object in all Page instances?

  • TommyRczTommyRcz Member ✭✭

    @ClintStLaurent said:

    The Token is generated after the connexion page that is actually the first

    Ok. And? You still put the Token object in the App class so all the other classes can reach it. Not seeing the problem.

    is there a way to make it static then

    Well. Yeah. Set it to static. What's that going to do for you though?

    Wait... thinking... Page constructors... static... Are you doing all this work in the Page code behind? Does this token live in the Page instead of in a ViewModel? And that's why you want it static so its the same object in all Page instances?

    How do you put the Token object in the App class ? The Token is user's account dependant, generated after the "connect" button of the first page loaded of the app, by a call to an online API, if you can tell me how to put this one to the App Class you'll have solved my problem.

    Thanks

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I have no idea what your buttons are doing... or how you've organized your app... Or how you're storing your User object... I have no insight into how you've architected your app. I can't tell you where to do what.

    But you're getting some sort of token object back, right? Add a property in the App class for it. All your other classes are able to reach the App class, so they can all look it up from there.

    Again... It sure sounds like you're doing all the work in the pages. Is that the case? Because that's just a really, really, really bad way to design your app.

  • TommyRczTommyRcz Member ✭✭

    Everything is not done in the pages of course.
    Well then, I'll simplify with a simple example: I make a call in a page, this call to the API is put into a simple string object. How can I put that string into the App class?

  • TommyRczTommyRcz Member ✭✭
    Accepted Answer

    Thanks a lot ! Putting my accessToken object in App Class solved my problemI don't know why I had not thought of that earlier, I was diving into darkness ;)

Sign In or Register to comment.