Let’s say I have a code
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var userInfo = SettingsManager.Read<User>(SettingsManager.UserInfoSetting);
if (e.NavigationMode == NavigationMode.Back && userInfo == null)
{
_mainViewModel.NavigationService.GoBack();
}
if (e.NavigationMode == NavigationMode.New && userInfo == null)
{
_mainViewModel.NavigationService.NavigateTo(new Uri(ViewModelLocator.SettingPageUrl, UriKind.Relative));
}
base.OnNavigatedTo(e);
}
When the user runs application for the first time he will be redirected to the settings page and it works pretty fine right now. If user doesn’t want to provide his information he can press back-button in that case I want to skip main page of the application and exit the application. If I run the code I received InvalidOperationException Cannot go back when CanGoBack is false.
The GoBack() method calls PhoneApplicationFrame.GoBack() method to navigate back.
You cannot forcibly exit this way.
What you could do, is handle the back button press on the settings page and clear the stack (which is allowed), then let the back button be handled — thus the user will exit because of the back button, not because of your call.