Windows Phone page value (7)

Source: Internet
Author: User

In Windows Phone, Microsoft provides a solution for passing parameters between pages. The following describes how to use this solution. In the case of passing values on pages, we create two pages, one is mainpage and the other is secondpage. The main code of the mianpage is:

 

<Grid X: Name = "contentpanel" grid. Row = "1" background = "Goldenrod" margin = "12,0, 12,0"> </GRID>
<Textblock X: Name = "navigation" text = "navigate to the second page" grid. row = "1" verticalalignment = "center" horizontalalignment = "center" manipulationstarted = "navigation_manipulationstarted"> </textblock>
</GRID>

Set the color of mainpage:

 

From the code above, we can see that the background of the grid attribute named contentpanel is set to a golden unicorn color. When you click the textblock element named navigation, pass the color of the contentpanel's grid attribute background to the second page. The hidden code of the navigation event manipulationstarted is:

private void Navigation_ManipulationStarted (object sender, ManipulationStartedEventArgs e)
         {
             // Get the color ARGB value
             SolidColorBrush scb = (SolidColorBrush) ContentPanel.Background;
             Color c = scb.Color;
             // Parameter passing format--knowledge points①
             Uri uri = new Uri ("/ SecondPage.xaml? Red =" + c.R + "& Green =" + c.G + "& Blue =" + c.B, UriKind.Relative);
             this.NavigationService.Source = uri;
                       

             e.Complete ();
             e.Handled = true;
         }

After the value to be passed on the page is set, it will navigate to the second page secondpage page. The main code in the secondpage. XAML file is:

 

 
<Grid X: Name = "contentpanel" grid. Row = "1" margin = "12,0, 12,0"> </GRID>
<Textblock X: Name = "navigation" text = "navigate to the-th page" grid. row = "1" verticalalignment = "center" horizontalalignment = "center" manipulationstarted = "navigation_manipulationstarted"> </textblock>
</GRID>

Secondpage effect:

 

From the code above, we can see that we do not set the color value of the grid attribute background named contentpanel. Here we will set the background color from the hidden file, the hidden code of the navigation event manipulationstarted is:

// textblock navigation time
         private void Navigation_ManipulationStarted (object sender, ManipulationStartedEventArgs e)
         {
             this.NavigationService.Navigate (new Uri ("/ MainPage.xaml", UriKind.Relative));
             e.Complete ();
             e.Handled = true;
         }


 

Onnavigatedto:

// After the active page constructor-knowledge point ②
         protected override void OnNavigatedTo (System.Windows.Navigation.NavigationEventArgs e)
         {
             // Get the value passed in the form of generic collection-knowledge point ③
             IDictionary <string, string> para = this.NavigationContext.QueryString;
             if (para.Count> 0)
             {
                 byte r = Byte.Parse (para ["Red"]);
                 byte b = Byte.Parse (para ["Blue"]);
                 byte g = Byte.Parse (para ["Green"]);
                 ContentPanel.Background = new SolidColorBrush (Color.FromArgb (255, r, g, b));
                
             }
           
         } 


This method is used to obtain the parameter passing the class from mainpage. We will parse it and set the contentpanel. Background attribute in secondpage Based on the parsed result. This will complete the parameter transfer;

 


 

Here, the format of parameter passing is worth noting. The parameter is followed by a question mark (?) after the relative Uri (?), It is followed by a key-Value Pair (Key = value). If there are multiple parameters, separate them with an & symbol.


 

The onnavigatedto method is used here. The official introduction is that when a page changes to an activity page in the framework, it can be understood that when a page is switched to B, this method is called when the constructor on the activity page is loaded. This means that the constructor will immediately execute this method after the constructor is executed.


 

The navigationcontext class is used to obtain the navigation status. The only property querystring is a set of query string values. Obtain the value of value based on the key in the returned collection type.


 

Remember: multiple parameters must be separated by the & Symbol. The onnavigatedto method that is overwritten when parameters are accepted on the activity page. This. navigationcontext. querystring accepts all parameters and values passed to this page, and returns a dictionary container

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.