Dealing with OnNavigatedTo/Passing parameters to MainPage on startup.
The situation is: We have a Windows Store app we are porting. On launching the app we use code to figure out from roaming preferences to determine to which page we navigate them, and also, depending on the page, pass some parameters to personalize it. Prism makes that so easy…just load the settings, add some logic to determine which page to send them, and then use the navigation service to get them to the right place with the right personalization options. The simply put all the setup logic for each ViewModel into OnNavigatedTo and you’re done.
From what I can see due to the way Xamarin works, Prism overrides the the MainPage call to return a specific page. I can see how I can move the logic from the Windows Store app to the BootStrapper MainPage override to determine to which page I navigate, but I don’t see a way to pass a parameter to it since we don’t use the navigation service for the main page.
Also it seems from testing, the OnNavigatedTo event doesn’t trigger when MainPage() is run (which makes sense because we didn’t get there via the navigation service). This presents a dilemma for where one should put any setup/data loading actions for the main page. While I could use OnAppearing() to handle setup stuff for it, that leaves me without the option to simply pass a parameter when another page calls it via the navigation service, unless I do something even more messy by splitting things up between OnAppearing and OnNavigatedTo, which could make a huge mess of things and doesn’t solve the issue of how do I get a parameter to the page when the app is first run.
For the main page, is there a “right way” to handle this?
I will have to research if an application with no MainPage has a functional INavigation that can be used. If a MainPage is required before a navigation can occur, then this will make for a very interesting dilemma. For now, you may be able to Override InitialzieMainPage, grab the BindinContext, cast it as the expected ViewModel, or as INavigationAware, and then call the OnNavigatedTo manually passing your parameters.
K. So from that info here's my solution that handles my use case: Add the following to the Bootstrapper class:
NavigationParameters navParameters = new NavigationParameters();
protected override void InitializeMainPage()
{
((INavigationAware)App.MainPage.BindingContext).OnNavigatedTo(navParameters);
}
Since I'm casting to INavigationAware, which all my viewmodels implement I can use logic to set
return Container.Resolve<AnyPageIWant>();
And instead of passing whatever parameters I want via the navigationservice I have to add them to navParameters, which isn't all that bad. By doing it this way I can call whatever page I want and pass any parameters from the bootstrapper.
Thanks!!!
Another option would be to do that on the Page before you return it in the CreateMainPage method.
public override CreateMainPage()
{
var page = new MyPage();
//do stuff
return page;
}
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.