white smartphone

How to Pass data between Pages in Windows Phone ?

The NavigationContext provides the QueryString collection which can be used in the destination page to get the data from the URL .

To Send the Data to the different form , the source page has to pass the parameters via URL

like Page2.xaml?name=senthil .

How to Pass data between Pages in Windows Phone ?

You can send multiple parameters with the help of the ampersand symbol like below

Page2.xaml?firstname=senthil&lastname=kumar

The Sample Program used for demonstration includes 2 pages where page1.xaml is the source and page2.xaml is the destination page

How to Pass data between Pages in Windows Phone ?
How to Pass data between Pages in Windows Phone ?

The Page1 has a textbox and a Button . On the button click event , the Navigate method of the NavigationService along with the parameter .

private void button1_Click(object sender, RoutedEventArgs e)
{
     string urlWIthData = string.Format("/Page2.xaml?name={0}", txtName.Text);
     this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
}

In the Page2.xaml Page Loaded event , the parameter passed from the Page 1 is retreived .

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
     lblPage1Data.Text = this.NavigationContext.QueryString["name"].ToString();
}

When i attemped to retreive the parameter in the InitializeComponent() , i got the below error .

How to Pass data between Pages in Windows Phone ?

    2 Comments

  1. Yury
    February 6, 2012
    Reply

    do it on
    override protecter onnavigatedto
    method

  2. Shahid
    March 26, 2012
    Reply

    On the destination page , please override OnNavigatedTo as
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {

    string l = “”, lo = “”;
    bool v1 = NavigationContext.QueryString.TryGetValue(“l”, out l);
    bool v2 = NavigationContext.QueryString.TryGetValue(“lo”, out lo);

    base.OnNavigatedTo(e);
    }

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you’ll learn about the Win32 Error “0x000019E5 – ERROR_COULD_NOT_RESIZE_LOG” that you get when debugging system erors in...
In this post, you’ll learn about the error “CO_E_DBERROR 0x8004E02B” that is returned when working with COM based APIs or...
In this post, you’ll learn about the Win32 Error “0x000019D0 – ERROR_LOG_BLOCK_VERSION” that you get when debugging system erors in...