SIGN UP MEMBER LOGIN:    
ARTICLE

Different Ways of Passing Values Between Windows Phone 7 Pages

Posted by Deepak Sharma Articles | Windows Phone in C# February 08, 2012
In this article we will see various ways of passing data between Windows Phone 7 pages.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Deepak. You have presented your article very nicely.

Posted by Maria Johnson Feb 09, 2012

Nice article.

Posted by Abhi Kumar Feb 09, 2012

Thanks for sharing

Posted by Nitin Singh Feb 09, 2012

good approach...

Posted by Jessica Stephen Feb 09, 2012
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor