0
votes

I am struggling to understand how the PRISM INavigationService maps to the standard Navigation.PushAsync provided by Xamarin Forms.

I have an application that I have started using XF. It consists of a MasterDetailPage that is the home page and the "Detail" ContentPage contains a number of buttons that are used to then navigate to other functionality. Within the App.xaml.cs, I set the Application.MainPage to an instance of my home page wrapped within a NavigationPage "MainPage = new NavigationPage(new HomePage())". When I then want to display a functionality page, I call "Navigation.PushAsync(new NewPage())" and this shows the new page along with a toolbar containing a back button; I can then navigate back to my home page using either the back button on the device or using the back button in the toolbar. Note, the application is running on Android.

I then looked at integrating PRISM.Forms; I wanted to use this as I have used PRISM for WPF for many years and felt that it should prove beneficial. I am using the latest version (6.3).

I created a new application based upon the "PRISM Templates", and then replicated the differences into my existing project...

  • The "App" class is now based off of "PrismApplication" instead of "Application" Within "PrismApplication.RegisterTypes"
  • I call "Container.RegisterForNavigation" for all of the forms that I want to be able to navigate to At the end of "PrismApplication.OnInitialized"
  • I call "INavigationService.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(HomeMasterDetailPage)}");"
  • Where I previously called "Navigation.PushAsync", I call "INavigationService.NavigateAsync(nameof(NewPage))" Within "MainActivity" I call the "App" constructor with an "IPlatformInitializer"

So far, so good. I see my home page as expected. However when I then navigate to "NewPage", I have no back button in the toolbar (though I still have my "hamburger" menu from the MasterDetailPage) and if I use the back button on the device it returns me to the operating system. It seems that the page has been inserted as the "Content" section of the MasterDetailPage instead of navigating to it as a new child page.

I am sure that this is not by design and therefore something that I am misunderstanding but can somebody point me back in the right direction as otherwise I feel I will have to discard PRISM and look at an alternative.

I also have a problem when trying to introduce a login screen into the mix. I start by calling "INavigationService.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(LoginPage)}");" and then if login succeeds I call "INavigationService.NavigateAsync($"/{nameof(NavigationPage)}/{nameof(HomeMasterDetailPage)}");" to get to the home page; problem here is that when displaying the home page (a MasterDetailPage) I no longer see the "hamburger" button to open the "Master" portion of the page. I suspect that this is probably also related to my using the INavigationService incorrectly but if not I can raise it as a separate issue.

1

1 Answers

0
votes

Prism's INavigationService essentially maps to the Xamarin Forms INavigation in this way: INavigationService.NavigateAsync => INavigation.PushAsync (or PushModalAsync if you set the useModal flag to true), & INavigationService.GoBackAsync => INavigation.PopAsync.

The INavigationService uses the container to resolve any pages that you specify in the Uri that you pass it and then pushes them onto the Navigation Stack. You may want to check out the various samples available, including the "HamburgerMenu" project which shows the use of a MasterDetailPage and it uses a LoginPage.