Windows Phone development (7): Good Master steering turn: http://blog.csdn.net/tcjiaan/article/details/7281421

Source: Internet
Author: User

After the page-related topics are closed, let's talk about how to navigate between pages. In more cases, our applications won't have only one page, but there should be n, just as we do the development of desktop applications, there may be more than one form in one application (except for very simple programs). We need to handle switching between forms. Likewise, during web development, there may be more than one page for our web application. We need to handle switching between pages. Of course, this operation is also required in our mobile apps. We call it "navigation". As developers, we fully control switching between pages. So, we also became the app's "master ".

 

  1. how to navigate.One simple method for page navigation is to use the navigateuri attribute of the hyperlinkbutton control to specify the URI Of The XAML file of the target page to be navigated. Here is a simple example.

Create a WP application project, put a hyperlinkbutton control on the home page, set the value of navigateuri to/pagesecond. XAML, and create a new page named pagesecond. XAML.

[HTML]View plaincopyprint?

  1. <Hyperlinkbutton content = "Jump to page 2" Height = "78" horizontalalignment = "Left" margin = "126,86, 0, 0 "name =" hyperlinkbutton1 "verticalalignment =" TOP "width =" 216 "fontsize =" 32 "fontstyle =" normal "fontstretch =" normal"
  2. Navigateuri = "/pagesecond. XAML"/>

 

The second navigation method is implemented by code, such as the Click Event of a button.

[CSHARP]View plaincopyprint?

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

 

2. onnavigatedfrom and onnavigatedto methods.1. When the user is about to leave the current page, the onnavigatedfrom method will be called; 2. the onnavigatedto method will be called when the user is navigating from other pages to this page. I believe you can write more code and test it. This is a good understanding. A. Add the following code on the home page. [CSHARP]View plaincopyprint?

  1. // Leave the home page
  2. Protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
  3. {
  4. Base. onnavigatedfrom (E );
  5. System. Diagnostics. Debug. writeline ("***** has left the home page. ");
  6. }

B. Add the following code on the second page.

[CSHARP]View plaincopyprint?

  1. // Navigate to the second page
  2. Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
  3. {
  4. Base. onnavigatedto (E );
  5. System. Diagnostics. Debug. writeline ("***** hi, has arrived at the second page. ");
  6. }

 

Then, you can run the program and navigate to see what is in the "output" window?

 

3. How to pass parameters between pages.

In Android development, the intent object needs to pass content from one activity to another. However, in WP development, we only need to append parameters on the URI like a web page.

For example:/numbb. XAML? Pt1 = AAAA & pt2 = CCCCC.

 

Now, let's change the example just now and put a textbox on the home page. We want to pass the input content on this page to the second page.

[CSHARP]View plaincopyprint?

  1. Private void button#click (Object sender, routedeventargs E)
  2. {
  3. This. navigationservice. navigate (New uri ("/pagesecond. XAML? STR = "+ textbox1.text, urikind. Relative ));
  4. }

Retrieve data from the second page. Retrieve data from the second page.

[CSHARP]View plaincopyprint?

  1. // Navigate to the second page
  2. Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
  3. {
  4. Base. onnavigatedto (E );
  5. // The Name Of The passed parameter.
  6. String Pv = This. navigationcontext. querystring ["str"];
  7. This. textblock1.text = PV;
  8. }

 

 

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.