Windows Phone development (7): Always at the helm

Source: Internet
Author: User


After the page-related topics are closed, let's talk about how to navigate between pages. In more cases, our applicationsProgramThere won't be only one page, but there should be n. Just as we do in the Development of desktop applications, there may be more than one form in one application (except for simple programs ), we need to handle switching between forms. Similarly, during web development, our web application may have more than one page, and we need to process 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.


<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 method of navigation isCodeFor example, click an event of a button.


 



   private void button1_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 is called when you navigate 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.
 



// leave the main page
         protected override void OnNavigatedFrom (System.Windows.Navigation.NavigationEventArgs e)
         {
             base.OnNavigatedFrom (e);

             System.Diagnostics.Debug.WriteLine ("***** has left the main page.");
         }


B. Add the following code on the second page.


 



// navigate to the second page
         protected override void OnNavigatedTo (System.Windows.Navigation.NavigationEventArgs e)
         {
             base.OnNavigatedTo (e);

             System.Diagnostics.Debug.WriteLine ("***** Hi, you have reached 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.


 



   private void button1_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.


// navigate to the second page
         protected override void OnNavigatedTo (System.Windows.Navigation.NavigationEventArgs e)
         {
             base.OnNavigatedTo (e);

             // What is the name of the parameter passed, here will be taken by what name.
             string pv = this.NavigationContext.QueryString ["str"];
             this.textBlock1.Text = pv;
         } 













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.