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
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.