0

I am trying to understand Prism Xamarin navigation and how to relate to xamarin itself

Could somebody correct me where I am wrong?

    Xamarin                       Prism
    Navigation.PopAsync =   NavigationService.NavigateAsync(uri,
                                                            useModalNavigation: true)       
                        =   NavigationService.GoBackAsync

    Navigation.PushAsync =  NavigationService.NavigateAsync(uri,
                                                            useModalNavigation: false)       

Also in prism

Is NavigationService.NavigateAsync(uri,useModalNavigation: false)   

same as

NavigationService.GoBackAsync

Are they both doing the same thing?

OnNavigatingTo(NavigationParameters parameters) vs OnNavigatedTo(NavigationParameters parameters)

They both are fired after the constructor is fired. Any practical example when to use one and when to use another?

What kind of of logic do you place in there.Are they used when you want to load a form?Also generally what kind of validation you put there and why?

    public void OnNavigatedTo(NavigationParameters parameters)
    {
      if(parameters.GetValue<NavigationMode>(KnownNavigationParameters.NavigationMode) == NavigationMode.Back)
        {            
        }

    //or logic like 
    if ( parameters.ContainsKey("myId") )
        {
        }
   }

Why would I use parameters.ContainsKey("myId") or the navigationMode check.

I am just trying to understand how OnNavigatedTo/OnNavigatingTo should be used. If somebody could phrase a scenario in few words I will understand how to use these methods.

Many thanks in advance

developer9969
  • 4,628
  • 6
  • 40
  • 88
  • Have you looked up any of the videos by Brian Lagunas, they provide some great information about this topic. For example, [link]https://www.youtube.com/watch?v=DYRLcqG2BAY – Tintow Jul 27 '17 at 12:48
  • Thanks I did see that video but was very general and not going into details ,I guess very short time to show and explain too many things – developer9969 Jul 27 '17 at 12:50

1 Answers1

2

The calls aren't totally comparable because of the additional functionality of Prism (parameters, deep navigation etc) but in simple terms;

 Xamarin                       Prism
Navigation.PopAsync = NavigationService.GoBackAsync

Navigation.PushAsync =  NavigationService.NavigateAsync(uri, useModalNavigation: false)

Navigation.PushModalAsync = NavigationService.NavigateAsync(uri, useModalNavigation: true)

The deep navigation in Prism is also very powerful so you can navigate multiple pages at once

NavigationService.NavigateAsync("Page1/Page2/Page3");

which navigates to Page1 then Page2 then Page 3 and maintains the correct the navigation stack.

You can also replace the entire navigation stack by using a absolute uri

 NavigationService.NavigateAsync(new uri("www.myapp.com/LoginPage", UriKind.Absolute);

This is useful for Login/authentication scenarios where you want the user to login before they can get to any other pages.

The OnNavigatingTo() and OnNavigatedTo() methods are similar but they fire at different times. If you trace the calls you will see

{Navigate to Page1}
Page1ViewModel.Constructor
Page1ViewModel.OnNavigatingTo 
Page1ViewModel.OnNavigatedTo 
{Navigate to Page 2}
Page2ViewModel.Constructor
Page2ViewModel.OnNavigatingTo 
Page1ViewModel.OnNavigatedFrom 
Page2ViewModel.OnNavigatedTo 

so the OnNavigatingTo of the second page is called before the OnNavigatedFrom of the first page and then the OnNavigatedTo of the second page is called.

This allows you to place code at exactly the right points in the navigation flow depending on your requirements. If you're just moving from one page to another then the OnNavigatedTo is probably what you want.

Hope that helps.

Tintow
  • 248
  • 1
  • 2
  • 8
  • I would add that to better understand the Navigation you can take a look at the [samples](https://github.com/PrismLibrary/Prism-Samples-Forms), or try out the [QuickStart Templates](http://dansiegel.net/post/2017/07/16/prism-quickstart-templates) which give you a full working app and even uses the Popups Plugin. – Dan Siegel Jul 27 '17 at 20:12
  • @Tintow many thanks for your time and explanation I will look again at the samples. – developer9969 Jul 28 '17 at 07:38
  • @DanS. I have been learning a lot from your stackflow replies and your examples on github.I one of the things I do not get is what is the purpose of Prism.Logger .Does it actually output anything? I have noticed you used it in your examples and was trying to understanding if it has any value as a user of prism. – developer9969 Jul 28 '17 at 07:45
  • We have actually pulled the DebugLogger out of Prism since we ship Prism as a Release build it doesn't work. There is a ton of value in the logging output, and I will be writing up a post on Logging in the near future. You can see how to easily enable it in your app here: https://gist.github.com/dansiegel/13e2326c347c51f6a1d45d7b51649e22 – Dan Siegel Jul 31 '17 at 01:47