DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Navigating and Passing Data Between XAML Pages with Windows Phone

Navigating and Passing Data Between XAML Pages with Windows Phone

Andrea Haubner user avatar by
Andrea Haubner
·
Jan. 25, 12 · Interview
Like (0)
Save
Tweet
Share
10.75K Views

Join the DZone community and get the full member experience.

Join For Free

Up to this point I’ve only used a single XAML page for my examples. Most applications though do have more than one page. So, it’s time to take a look at how to navigate through more than one XAML pages in a Silverlight Phone application.

First of all, pages in a Silverlight application are loaded similar to a html page in a web browser. Each website has its own URL. To navigate for example from this blog entry to the homepage, there is a so called Anchor Tag needed. Silverlight applications are very similar, except that instead of an anchor tag I use a hyperlink button control, and then I set its NavigateUri property to the URI of the XAML page I want to load into that parent frame that is top most in my application. Sounds easy enough.

URL is a Unified Resource Locator, URI is a Unified Resource Identifier. Both are ways of finding and locating resources. One is specific to the internet, the other one is more generalized and can be used for internet, local, within a local network, on a phone and so on.

So, let’s get started. I’ve created a new project. Right click on my project lets me add a new folder which I call “Views”:

1

Then I am going to add a new page to that folder:

1a

I am going to make sure that I select Windows Phone Portrait Page in the opening dialogue box:

1b

Now I have two XAML pages in my project:

1d     1e

Back on MainPage.xaml I am going to drag and drop a HyperlinkButton control onto my designer surface. Now I need to modify the NavigateUri property. I start with the name of the project followed by a semicolon and then the component. I have my new page nicely in a subfolder, so the name of that folder is added next:

/NavigatingBetweenPages;component/Views/Page1.xaml

Let’s run that:

2     2a

When I click the Hyperlink Button I navigated from my Main Page to Page1. So far, so good. Now I want to pass some information between those two pages. So I am going to construct a query in my NavigateUri, I am also going to add a Querystring that will embed some additional information in that URI. And then I am going to retrieve that extra information on Page1.xaml and display it on the screen. In the World Wide Web, a query string is the part of a URL that contains data to be passed to web applications. Similarly, in a phone application a query string is the part of a URI that contains data that can be passed from page to page.

Let’s move on in my little example project. First I am adding the querystring to the NavigateUri property:

/NavigatingBetweenPages;component/Views/Page1.xaml?id=1


Then I am adding another Hyperlink Button and set the NavigateUri property to:

/NavigatingBetweenPages;component/Views/Page1.xaml?id=2


Now I am passing two different values, depending on which Hyperlink Button I choose to click.

Back on Page1,xaml I am going to add a textblock and clear out its text property. Then inside the PhoneApplicationPage of Page1 I am double clicking on the Loaded Event in the properties window to get to the code behind, in order to add some code:

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = String.Format("Value: {0}",
            NavigationContext.QueryString["id"]);
    }

 

When I run it now, I get this:

3a    3b    3c

I just wanted to display the value that I defined in the NavigateUri property before. Seems to work.

This example is not doing anything really exciting just yet, but I could use the same technique to do several interesting things. For example if I wanted to create a weather application and I wanted to allow the user to select a city and a zip code. I could pass those values from a XAML page, where the user actually selects the city and the zip code, and then on a second XAML page I could use some web service over WiFi or the phone’s network, and then on a third page retrieve those values and display them to the user.

There is only one problem with the code that I’ve been writing so far: If there is no querystring with the name of “id” passed in from the preceding page then my application is going to blow up. It is going to throw an exception and crash. So, I need to fix that problem.

And also, I am wondering, what about sending and retrieving two or more querystring named value pairs? How can I accomplish that in my code? Well, it is the same basic idea. I just need to separate the named value pairs on my MainPage.xaml’s Hyperlink Buttons.

So, I am going to add a third Hyperlink Button to my designer surface, and I am going to set the NavigateUri property to:

/NavigatingBetweenPages;component/Views/Page1.xaml?id=3&state=tx


Then I am going to add a second textblock to my Page1.xaml’s designer surface and clear its Text property. Next I am going to my Page1.xaml.cs and add the following code into the Loaded Event:

    string id = "";
     
    if (NavigationContext.QueryString.TryGetValue("id", out id))
    {
        textBlock1.Text = String.Format("Value: {0}", id);
    }
     
    string state = "";
     
    if (NavigationContext.QueryString.TryGetValue("state", out state))
    {
        textBlock2.Text = String.Format("State {0}", state);
    }

 

By adding this code I fixed both of my problems.

The earlier solutions left me with the possibility that there is no id defined which would have caused the application to crash. Now, with the TryGetValue method, it is simply trying to retrieve the id value and throw it out. If it can’t find that value it will skip that code block and leave that out. The same is true for the second part of the code. It will try to retrieve state as an output parameter. If it can get “state” then it will execute the second code block.

In other words, if I run this code, and click the first Hyperlink Button, it should only show the value in textblock 1, if I click the third one, it should show both values in both textblocks. Let’s see, if that is true:

3b     3d

Seems to work like it is supposed to.

 

 

To be continued…



Source: http://andreahaubner.blog.com/2011/03/19/navigating-and-passing-data-between-xaml-pages/

Windows Phone application Data (computing) Property (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Rust vs Go: Which Is Better?
  • mTLS Everywere
  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • How Elasticsearch Works

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: