WP7 knows when the back key is pressed on the Navigation page

On Windows Phone 7, is there a way to find out if the back of the page that I clicked on is clicked? I know that we can intercept the current page, but I need to know on the page that I'm switching to. that is, if 2 pages speak on page 1 and page 2, on page 2 the "Back" button is pressed. I need to know if the button is pressed back or not on page 1. I need to start something on the back button by pressing on page 1.

+4
source share
6 answers

This is a kind of hack, but you can do the following: override the OnBackkeyPress event on each page. Inside the event handler, add the following code:

 PhoneApplicationService.Current.State["isbacknav"] = true; 

Then, in the OnNavigatedTo event OnNavigatedTo for each page, verify that the State dictionary contains this 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" ); } 
+5
source
 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back) ... } 
+25
source

On each page, you can override the OnBackKeyPress event to detect a key pressed.

On this second page, you can set a global flag or send a message to the first page.
There is no way to know which page was opened / displayed by the last page. You get it yourself.

0
source

Unfortunately, I do not think there is a built-in way to do this.

When you learn about OnNavigatingFrom, the event arguments include NavigationMode - http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigatingcanceleventargs.navigationmode(v=vs.95 )

However, when you learn about OnNavigatedTo, args events do not give you this 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, check this carefully, as there may be problems with which code gets in some order. In addition, there may be problems with the correct calling of this code when a user goes to a page from the phone’s home page or from another application.

Good explanation of NavigationService.Navigating at http://wildermuth.com/2010/10/11/Architecting_WP7_-_Part_1_of_10_Navigation_Framework

0
source

I had exactly the same problem. You can see the answers here.

I did not want to reload the data if I went back to the page where I already downloaded the data. In the end, I had to use flags to determine if I had already loaded the data.

0
source

You can always define the bool / s variable in the App class (App.xaml.cs). they can be accessed from any page in the following way.

in class App

public bool backVariable=false;

on page 1 Page1.xaml.cs globally or in any way.

App thisApp=Application.Current as App;

then u can access the boolean variable with

thisApp.backVariable

0
source

Source: https://habr.com/ru/post/1344674/


All Articles