Reputation: 21
I'm trying to prevent the user from back navigation on prism xamarin forms.
I'm having difficulty because, when I get the event, the app changes the page.
How do I block back navigation on prism navigation?
Upvotes: 1
Views: 1894
Reputation: 301
You can implement the IConfirmNavigationAsync or IConfirmNavigation interface in your ViewModel which gives you a method that returns a bool to allow or block navigation. You can then use the GetNavigationMode to determine if it is a backwards navigation:
Non-async method:
public class MyViewModel : BindableBase, IConfirmNavigation
{
public bool CanNavigate(INavigationParameters parameters)
{
/* Prevent backward navigation */
if (parameters.GetNavigationMode() == NavigationMode.Back) return false;
return true;
}
}
Async method (Great for showing confirmation popup before navigating back for example):
public class MyViewModel : BindableBase, IConfirmNavigationAsync
{
public async Task<bool> CanNavigateAsync(INavigationParameters parameters)
{
/* Confirm unsaved changes */
if (parameters.GetNavigationMode() == NavigationMode.Back)
{
var confirmed = await alertService.DisplayAlertAsync("Unsaved Changes", "You have unsaved changes do you wish to continue?");
return confirmed;
}
return true;
}
}
Upvotes: 1
Reputation: 1009
You can try to intercept the back event. One way is to implement the interface INavigationAware in your ViewModel:
public void OnNavigatedTo(NavigationParameters parameters)
{
if(parameters.GetNavigationMode()== NavigationMode.Back){
//Here your code
}
}
Or take a look to this discussion:
Upvotes: -1