Friday, June 3, 2016

Lesson 3: Windows Phone Application: Navigation between pages


Open Visual Studio 2013. Open new Project and select windows Phone Application from the options. Select C# programming language from the list. 
 In this article i will show you how to navigate from one page to another page. Here, we need two pages. The first page accepts two numbers and the multiplication result is displayed in the second page. This simple example is considered to show you how navigation is done from one page to another.

Create the pages as shown below.Drag and drop the controls as shown in the figure. First page has two textbox and one button. The second page has one textbox and one button. The controls are named accordingly and labels are used against them to increase readability.



Page 1

Page2


6.       Double click  on the btnMultiply button on the first page . MainPage.xaml.cs  (this is the code area)  window will be opened . Type the following code. 
       
     private void btnMultiply_Click(object sender, RoutedEventArgs e)
        {
            float a, b, c;
            a = float.Parse(txtNumber1.Text);
            b = float.Parse(txtNumber2.Text);
            c = a * b;
   NavigationService.Navigate(new Uri("/Result.xaml?msg="+c,UriKind.Relative));
            
        }

     Double click the btnGoBack button on the second page. MainPage.xaml.cs   window will be opened . Type the following code.  
    
      private void btnGoBack_Click(object sender, RoutedEventArgs e)
        {

            NavigationService.GoBack();
        }

     Add the following code in OnNavigatedTo() Method.

     protected override void OnNavigatedTo(System.Windows .Navigation.                                                                     NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string msg = "";

            if (NavigationContext.QueryString.TryGetValue("msg", out msg))

                txtResult.Text = msg;
        }  
   
      Here, the btnMultiply_click method is used to accept two numbers from two     textbox and then multiply these numbers and store the result in the third    
       number.
     
       The btnGoBack_click method is used to navigate from page2 to page1.

       The OnNavigatedTo() method is used to pass the result from page1 to     
        page2    and display the result in a textbox.


No comments:

Post a Comment