In the previous article, I used several examples to briefly introduce navigation. In general applications, I used the previous articleArticleThe method mentioned in is actually enough. However, in order to handle some special situations, it is necessary to have a few tips.
1. Should I set the "back" operation?
Because there is a "back-to-back" button on the hardware layer of the mobile phone, we do not need Program But it is necessary to see how to manually add the rollback function.
1. Create a WP application project.
2. In addition to the default homepage, create a page page2.xaml.
3. Put a button on the home page and write Click Event ProcessingCode, And navigate to page2.
Private void button#click (Object sender, routedeventargs e) {This. navigationservice. navigate (New uri ("/page2.xaml", urikind. Relative ));}
4. Put a button in page2 and write the event processing code. 4. Put a button in page2 and write the event processing code. 4. Put a button in page2 and write the event processing code.
Private void button#click (Object sender, routedeventargs e) {This. navigationservice. Goback ();}
5. Now, you can enjoy your program.
2. How can I block the "back" button?
This case is rare. Blocking the rollback button means that you cannot press "rollback" to perform backward navigation. This method should be used with caution.
To complete this operation, you must handle the backkeypress event and set the cancel attribute of event parameter E to true to cancel the "rollback" key operation.
This. backkeypress + = (sender, e) => {e. Cancel = true ;};
3. How to delete navigation history records?
For example, I now navigate from the home page to page B, and then from page B to Page C, but I do not want the user to navigate back to page B, but directly back to the home page.
Prepare three pages for test. navigate from the home page to B and from B to C. It should be okay. no need to repeat them.
Then, because we want to delete page B from the rollback history record of the navigation, we will delete the history record after leaving page B. That is to say, rewrite the onnavigatedfrom method in page B.
Protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs e) {base. onnavigatedfrom (E); phoneapplicationframe myframe = application. Current. rootvisual as phoneapplicationframe; If (myframe! = NULL) {try {myframe. removebackentry ();} catch (invalidoperationexception ex) {MessageBox. Show (ex. Message );}}}
From the example, we can see that the removebackentry method of the phoneapplicationframe class is used to delete the latest record. Each time only one record is deleted, n times are called to delete multiple records. Because the navigation history record is in the stack structure, it is as if you put a pile of books on the desktop, the first thing you remove is put at the top, as shown in: from the example, use the removebackentry method of the phoneapplicationframe class to delete the latest record. Delete only one record at a time. To delete multiple records, call it n times. Because the navigation history is in the stack structure, you can take a pile of books on the desktop, as shown in: