Different ways of passing values between Windows Phone 7 pages

In this article we will see different ways of passing data between Windows Phone 7 pages

Introduction

In this we will explore different ways of passing data between Windows Phone 7 pages. Let us assume that we have two pages in our application with name “Page1.xaml” and “Page2.xaml”. We will try to pass value from Page1 to Page2 using different methods.

Using Fragments

We can use fragments in URL to pass value from Page1 to Page2.

In “Page1.xaml.cs”, write following code in a Button click event:

private void btnGo_Click(object sender, RoutedEventArgs e)
{
     this.NavigationService.Navigate(new Uri("/Page2.xaml#Deepak"), UriKind.Relative);
}

To get value in “Page2.xaml.cs”, override OnFragmentNavitation method

protected override void OnFragmentNavigation(FragmentNavigationEventArgs e)
{
     MessageBox.Show(e.Fragment);
}

Using QueryString

We can pass only one value using fragment. To pass multiple values we should use QueryString

Code to pass values in Page1.xaml.cs:

private void btnGo_Click(object sender, RoutedEventArgs e)
{
     this.NavigationService.Navigate(new Uri("/frmMap.xaml?FirstName=Deepak&LastName=Sharma", UriKind.Relative));
}

Code to get values in Page2.xaml.cs:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
     string FirstName = NavigationContext.QueryString["FirstName"];
     string LastName = NavigationContext.QueryString["LastName"];
     MessageBox.Show(FirstName + ' ' + LastName);
}

Using public property in App.xaml

It acts as declaring a static global variable in “App.xaml.cs” and using it from any part of the application.

Declare a public property in “App.xaml.cs”

public string MyName { get; set; }

Now we can access this “MyName” property from anywhere in the application using “App.Current” object

Code to set MyName value  in Page1.xaml

private void btnGo_Click(object sender, RoutedEventArgs e)
{
     var obj = App.Current as App;
     obj.MyName = "dpkshr";
     this.NavigationService.Navigate(new Uri("/frmMap.xaml", UriKind.Relative));
}

Code to get MyName value in Page2.xaml

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
     var obj = App.Current as App;
     MessageBox.Show(obj.MyName);
}

Using OnNavigatedFrom method

OnNavigateFrom is called when we call NavigationService.Navigate method. It has a NavigationEventArgs object in its parameter that returns the destination page with its Content property. Through which we can access a property of the destination page “Page2.xaml.cs”

First in destination page “Page2.xaml.cs”, declare a property “MyAddress”

public string MyAddress { get; set; }
<strong> 

Now, in “Page1.xaml.cs”, override OnNavigatedFrom metod

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
     // NavigationEventArgs returns destination page “Page2”
     Page2 obj = e.Content as Page2;
     if (obj != null)
     {
          // Change property of destination page “Page2”
          obj.MyAddress = "New Delhi";
     }
}

Now, to get MyAddress value in “Page2.xaml.cs”

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
     // This will display "New Delhi"
     MessageBox.Show(MyAddress);
}

Conclusion

In this article we saw different methods of passing values in Windows Phone 7 pages. We can use any of the method as par our convenience.

Thanks!!!

One thought on “Different ways of passing values between Windows Phone 7 pages

  1. Gireesh

    how to access the page1 controls from page2(ie., from dest page)?

    how to pass the object set in page2 to page1 when back button pressed in page2 ?

    Reply

Leave a Reply

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