Windows Phone development (8): Tips for navigation turn: http://blog.csdn.net/tcjiaan/article/details/7285062

Source: Internet
Author: User

In the previous article, I used a few examples to briefly introduce navigation. In general applications, the method mentioned in the previous article is actually enough. However, in order to be able to handle some special situations, there are a few tips to learn about.

1. Should I set the "back" operation?
Because there is a "back-to-back" button on the hardware layer of the mobile phone, it is reasonable to say that we do not need to add any more rollback buttons in the program. However, 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, write the Click Event Processing code, and navigate to page2.

 

[CSHARP]View plaincopyprint?

  1. Private void button#click (Object sender, routedeventargs E)
  2. {
  3. This. navigationservice. navigate (New uri ("/page2.xaml", urikind. Relative ));
  4. }

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.

[CSHARP]View plaincopyprint?

  1. Private void button#click (Object sender, routedeventargs E)
  2. {
  3. This. navigationservice. Goback ();
  4. }

 

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. [CSHARP]View plaincopyprint?

  1. This. backkeypress + = (sender, e) =>
  2. {
  3. E. Cancel = true;
  4. };

 

 

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.

 

 

[CSHARP]View plaincopyprint?

  1. Protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
  2. {
  3. Base. onnavigatedfrom (E );
  4. Phoneapplicationframe myframe = application. Current. rootvisual as phoneapplicationframe;
  5. If (myframe! = NULL)
  6. {
  7. Try
  8. {
  9. Myframe. removebackentry ();
  10. }
  11. Catch (invalidoperationexception ex)
  12. {
  13. MessageBox. Show (ex. Message );
  14. }
  15. }
  16. }

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:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.