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
- <StackPanel>
- <TextBox InputScope="Chat" Name="TextBoxUsername"></TextBox>
- <Button Click="Button_Click">Signup</Button>
- </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
- private void Button_Click(object sender, RoutedEventArgs e)
- {
-
- NavigationService.Navigate(new Uri("/Page1.xaml?key1=" + TextBoxUsername.Text, UriKind.Relative));
- }
Defined a click event in the Mainpage.xaml.cs and using NavigationService.navigate property navigating to Page1.xaml using a query string.
Page1.xaml
- <StackPanel Grid.Row="0" Margin="12,17,0,28">
- <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock Name="TextBlockUsername" Text="" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </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.
- public partial class Page1 : PhoneApplicationPage
- {
- public Page1()
- {
- InitializeComponent();
- this.Loaded += Page1_Loaded;
- }
-
- private void Page1_Loaded(object sender, RoutedEventArgs e)
- {
- string val;
- if (NavigationContext.QueryString.TryGetValue("key1", out val))
- {
- MessageBox.Show(val, "Information", MessageBoxButton.OK);
- }
-
- }
- }
For More visit..