WP7 know when back key is pressed in the "navigated to" page
In Windows Phone 7, is there a way to know if the back is pressed in the page that's navigated to? I know we can intercept in the current page but I need to know in the page I am navigating to. i.e. if there 2 p开发者_JAVA百科ages say page1 and page2, back button is pressed in page2. I need to know if back button is pressed or not in page1. I need to run some stuff on back button press in page1.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
...
}
This is kind of a hack but you can do the following; override OnBackkeyPress
event on every page. Within the event handler add the following code:
PhoneApplicationService.Current.State["isbacknav"] = true;
Then, in the OnNavigatedTo
event handler for every page, check whether the State
dictionary contains that entry.
bool isbacknav = false;
if( PhoneApplicationService.Current.State.ContainsKey( "isbacknav" ) ) {
isbacknav = (bool)PhoneApplicationService.Current.State["isbacknav"];
PhoneApplicationService.Current.State["isbacknav"] = false;
// or
// PhoneApplicationService.Current.State.Remove( "isbacknav" );
}
Sadly I don't think there's a built in way to do this.
When you get told about OnNavigatingFrom then the event args includes NavigationMode - http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigatingcanceleventargs.navigationmode(v=vs.95)
However, when you get told about OnNavigatedTo then the event args don't give you that information.
I think the easiest workaround might be to add a handler for NavigationService.Navigating
in your App class - you can cache the NavigationMode there. However, test this thoroughly as there may be issues about what code gets in what order - plus there may be problems with whether this code is correctly called when the user navigates back to your page from the Phone home page or from another app
There's a good explanation of NavigationService.Navigating
in http://wildermuth.com/2010/10/11/Architecting_WP7_-_Part_1_of_10_Navigation_Framework
On each page you can override the OnBackKeyPress
event to detect the back key being pressed.
In that second page you could set a global flag or send a message to the first page.
There's no way for a page to know what the last page that was opened/displayed was. You'll have you to this yourself.
I had exactly the same problem. You can see the answers here
I didn't want to reload data if I was navigating back to a page where I had already loaded data. In the end I had to use flags to determine if I had already loaded data.
you can always define a bool variable/s in the App class( App.xaml.cs ). they can be accessed from any page by the following method
in App class
public bool backVariable=false;
in page1 Page1.xaml.cs globally or in any method
App thisApp=Application.Current as App;
then u can access the boolean variable using
thisApp.backVariable
精彩评论