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?
- <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"
- Navigateuri = "/pagesecond. XAML"/>
The second navigation method is implemented by code, such as the Click Event of a button.
[CSHARP]View plaincopyprint?
- Private void button#click (Object sender, routedeventargs E)
- {
- This. navigationservice. navigate (New uri ("/pagesecond. XAML", urikind. Relative ));
- }
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?
- // Leave the home page
- Protected override void onnavigatedfrom (system. Windows. Navigation. navigationeventargs E)
- {
- Base. onnavigatedfrom (E );
- System. Diagnostics. Debug. writeline ("***** has left the home page. ");
- }
B. Add the following code on the second page.
[CSHARP]View plaincopyprint?
- // Navigate to the second page
- Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
- {
- Base. onnavigatedto (E );
- System. Diagnostics. Debug. writeline ("***** hi, has arrived at the second page. ");
- }
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?
- Private void button#click (Object sender, routedeventargs E)
- {
- This. navigationservice. navigate (New uri ("/pagesecond. XAML? STR = "+ textbox1.text, urikind. Relative ));
- }
Retrieve data from the second page. Retrieve data from the second page.
[CSHARP]View plaincopyprint?
- // Navigate to the second page
- Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
- {
- Base. onnavigatedto (E );
- // The Name Of The passed parameter.
- String Pv = This. navigationcontext. querystring ["str"];
- This. textblock1.text = PV;
- }