OnBackKeyPress going to another instance of a page
Is there a way to handle OnBackKeyPress in such way that it returns to the actual page in the b开发者_如何学Cack-stack instead of going back to another instance of the same page? This is the code I have:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
base.OnBackKeyPress(e);
this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
But it doesn't do what I want. is there a way to do this without using the NavigationService and getting the actual page in the back-stack instead?
Thanks.
The short answer is no you can't do that. Other than using the NavigationService to move backwards page by page there is no way to manipulate the back stack. Rightly or wrongly this is by design. And it kind of makes sense as they need to sure that the operation of the back button is consistent.
What you are trying to do is actually change the behavior of the back button. Which is not what the OnBackKeyPress is intended for. The WP7 Design Guide says the following about the back button:
The back button can close an on-screen keyboard, menus, dialogs, navigate to a previous page, exit a search operation or even switch between applications. However the principal usage is to move from a current screen to a previous screen.
If your scenario is not covered in the by the above I would suggest maybe you are trying to do something which is fundamentally in consistent with Microsoft's intentions for the back button.
If your reason for wanting this behavior is to maintain state on your previous page when navigating back, you can achieve this using dependency injection and the MVVM pattern. You can register a singleton/instance of your viewmodel in your dependency container. That way when your view gets navigated to via the back button, you can inject that single instance of the viewmodel into the view and have the previous state if you rely on the binding techniques of MVVM.
I know that there's a phone version of Ninject floating around for a dependency container so you don't need to roll your own injection container (just search for Ninject wp7).
I was able to fix it. This was the problem:
MainPage -> SettingsPage -> SetLocationPage
Once location was set, we sent the user back to SettingsPage with NavigationService.Navigate
, then when the user pressed the back button in the SettingsPage it would go back again to the SetLocation, when it made more sense to send it to the MainPage.
This is how it was:
SetLocation -> SettingsPage -> (Back button) -> SetLocationPage
Now, I'm using NavigationService.GoBack
instead of NavigationService.Navigate
in the SetLocationPage:
SetLocationPage (once location is set use GoBack()) -> SettingsPage (use GoBack here too) -> MainPage.
Navigation now makes sense, and I solved it using NavigationService.GoBack()
.
Thanks for everyone's input.
I'm curious what you trying to do here and why you need to navigate to implement back.
Ordinarily you dont implement the override and the user presses back, and the NavigationService automagically takes the user to the previous bage in the back stack.
If this is not the case can you elaborate on why. There is some information that may help, but I suspect it would be a band-aid on top of an existing problem at the moment.
精彩评论