Reader Level:
Articles

Different Ways of Passing Values Between Windows Phone 7 Pages

By Deepak Sharma on February 08, 2012
In this article we will see various ways of passing data between Windows Phone 7 pages.
  • 0
  • 0
  • 28162

Introduction

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

Using Fragments

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

In "Page1.xaml.cs", write the 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 a value in "Page2.xaml.cs", override the OnFragmentNavitation method:

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

Using QueryString

We can pass only one value using a 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 the "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 the NavigationService.Navigate method. It has a NavigationEventArgs object as a parameter that returns the destination page with its Content property with which we can access a property of the destination page "Page2.xaml.cs"

First, in the destination page "Page2.xaml.cs", declare a property "MyAddress":

public string MyAddress { get; set; }

Now, in "Page1.xaml.cs", override the OnNavigatedFrom method:

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, get the 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 various methods of passing values in Windows Phone 7 pages. We can use any of the methods as per our convenience.

Deepak Sharma

Deepak is a Software Developer with over 4 years of experience. His expertise is in Windows Forms, ASP.NET, WPF, SQL Server and MySql.Twitter : @idpkshrBlog : http://deepak-sharma.net

Personal Blog: http://deepak-sharma.net
COMMENT USING

Trending up