Passing parameters while navigating between pages on Windows Phone
This article shows how to pass parameters during page navigation on Windows Phone 7.
NavigationService enables to download content within the context of browser style navigation. There are 2 ways to navigate content:
- To navigate the content we need to use Navigate() method of NavigationService and passing the object to it.
- NavigationService.Navigate(Object)
- NavigationService.Navigate(Object, Object)
- To navigate the content between pages we can use another overload of Navigate() method which takes a uniform resource identifier (URI) as one of the parameters and send the object to this URI:
- NavigationService.Navigate(Uri)
- NavigationService.Navigate(Uri, Object)
- NavigationService.Navigate(Uri, Object, Boolean)
When content is navigated through a URI, NavigationService will return an object that contains the content.
Code Example
Let’s see an example of how to navigate content between pages.
Consider we have 2 pages – page1, page2. On click of submit button on page1,we need to send some data to Page2.
Code for submit button on Page1
private void submit_click(Object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri(“/Page2.xaml?msg=”+ txtName.text,Uri.Relative) );
}
Code for retrieving data on Page2
protected override void onNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.onNavigatedTo(e);
string msg;
if(NavigationContext.QueryString.TryGetValue(“msg”,out msg))
{
string name = msg;
}
}


(no comments yet)