Search code examples
c#xamarinxamarin.formsprismprism-7

In Prism 7 for Xamarin Forms, is there a way to get prior page in OnNavigatedTo handler


New to using Prism in Xamarin Forms: INavigationAware gives me the OnNavigatedTo handler. I want different behavior in this form depending on where we are navigating from. Is there a way to get the prior page?

Specifically I have page A that opens page B (modal) if a certain condition is true. However, if the back button is hit from page B, returning me to Page A, I don't want to open page B again. Rather, I want to go back to the prior page beneath Page A on the navigation stack.


Solution

  • Well passing the Page would be in direct violation of MVVM design, as your ViewModel should have no direct clue about the associated View. That said all of the INavigationAware methods use NavigationParameters which means that you can pass some value that you can check for similar to the following:

    NavigationService.NavigateAsync("ModalA", new NavigationParameters {{"reason", "foo"}});
    
    public void OnNavigatedTo(INavigationParameters navigationParameters)
    {
        switch(navigationParameters.GetValue<string>("reason"))
        {
            case "foo":
                // Do Foo
                break;
            case "bar":
                // Do Bar
                break;
        }
    }