Data Passing From One Page to Another in XAML

Data passing means to get data from a page and show that data on another page or getting input from the user and showing what the user has entered into the text box. For that I have added the two pages MainPage.xaml and page1.xaml and I will enter some text into the text box and on clicking Enter or the the submit button it will show the entered text in the second page.

This Is The MainPage.Xaml

  1. <StackPanel>    
  2.         <TextBox InputScope="Chat" Name="TextBoxUsername"></TextBox>    
  3.         <Button Click="Button_Click">Signup</Button>    
  4. </StackPanel>   
In the mainpage.xaml I have put a stack panel and in the stack panel I have used two controls, a TextBox control and a Button control. Here when I enter any text into the text box and click the button it will navigate to page1.xaml and show the entered text in the Page1.xaml as a message dialog.

MainPage.xaml.cs
  1. private void Button_Click(object sender, RoutedEventArgs e)    
  2.        {    
  3.    
  4.            NavigationService.Navigate(new Uri("/Page1.xaml?key1=" + TextBoxUsername.Text, UriKind.Relative));    
  5.        }    
Defined a click event in the Mainpage.xaml.cs and using NavigationService.navigate property navigating to Page1.xaml using a query string.

Page1.xaml
  1. <StackPanel Grid.Row="0" Margin="12,17,0,28">    
  2.             <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>    
  3.             <TextBlock Name="TextBlockUsername" Text="" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>    
  4. </StackPanel>   
In Page1.xaml I have use some controls to show the incoming data.

Page1.xaml.cs

In Page1.xaml.cs under the initialize component Page1_loaded method was called and in the Page1_loaded method definition a string variable val was declared to return the incoming data in the string variable val and showing that data in a message dialog box.
  1. public partial class Page1 : PhoneApplicationPage      
  2.  {      
  3.      public Page1()      
  4.      {      
  5.          InitializeComponent();      
  6.          this.Loaded += Page1_Loaded;      
  7.       }      
  8.       
  9.      private void Page1_Loaded(object sender, RoutedEventArgs e)      
  10.      {      
  11.          string val;      
  12.          if (NavigationContext.QueryString.TryGetValue("key1"out val))      
  13.          {      
  14.              MessageBox.Show(val, "Information", MessageBoxButton.OK);      
  15.          }      
  16.            
  17.      }      
  18.  }     

 
 
                                                                                                                                 For More visit.. 


Recommended Free Ebook
Similar Articles