Navigation between pages in Windows 7 Phone

To see how could we do navigation between pages in Windows 7 Phone application , I am going to create a new page . I am giving name of the page as NewPage.Xaml

Now we will navigate as

clip_image001

Let us put a button on MainPage.xaml and on click of that button we will navigate tp NewPage.Xaml

clip_image003

On click event,


void btnToNavigate_Click(object sender, RoutedEventArgs e)
 {

 NavigationService.Navigate(new Uri("/NewPage.xaml", UriKind.Relative));

 }

 

There is one more way to navigate to other page

 


void btnToNavigate_Click(object sender, RoutedEventArgs e)
 {

 PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
 frame.Navigate(new Uri("/NewPage.xaml", UriKind.Relative));
 }

 

 

Now if we want to pass value from one page to another page then,


void btnToNavigate_Click(object sender, RoutedEventArgs e)
 {
 string param1="Value1";
 string param2="Value2";
 NavigationService.Navigate(new Uri(string.Format("/NewPage.xaml?val1={0}&val2={1}",param1 ,param2), UriKind.Relative));

 }

 

In above code, we are passing param1 and param2 to next page.

Now to fetch these values from query parameter on NewPage.Xaml.cs , we need to override the onNavigated() method.

And code would be like

 protected  override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs  e)
 {
 base.OnNavigatedTo(e);
 string value1 = "";
 string value2 = "";
 NavigationContext.QueryString.TryGetValue("val1", out value1);
 NavigationContext.QueryString.TryGetValue("val2", out value2);
 MessageBox.Show(value1);
 MessageBox.Show(value2);
 }

Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

One thought on “Navigation between pages in Windows 7 Phone”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s