Passing Parameters in Windows Phone Application

This topic can be explained with an example as:

On main page of your project add a StackPanel. Within this StackPanel add TextBlock and a Button control as shown in the XAML code below:

<Grid>
        <StackPanel>
            <TextBlock>Main Page</TextBlock>
            <TextBox Name="txtbox"></TextBox>
            <Button Content="Pass Param" Name="navigate" Click="navigate_Click"></Button>
        </StackPanel>
</Grid>


The designer view for this is as under:


Code for the xaml.cs file is as below:

namespace PassingParameters
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private void navigate_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + txtbox.Text, UriKind.Relative));

        }
    }
}

Now add a second page to your project. And the xaml code for the second page is as below:

<Grid>
        <StackPanel>
            <TextBlock Name="txtblock" TextWrapping="Wrap" Text=""/>
            <Button Content="Back to Home" Click="Button_Click"></Button>
        </StackPanel>
</Grid>

The designer view for the second page is as under:


Now the code for the secondPage.xaml.cs is as below:

namespace PassingParameters
{
    public partial class SecondPage : PhoneApplicationPage
    {
        public SecondPage()
        {
            InitializeComponent();
        }

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

            string msg = "";

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

                txtblock.Text = msg;
           
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }
    }

Passing Parameters in Windows Phone Application Passing Parameters in Windows Phone Application Reviewed by Afsar Murad on 01:48 Rating: 5
Powered by Blogger.