DCSIMG
WP7 - Data Between Pages – Part 1 - Shai Raiten's Blog

Shai Raiten's Blog

It's all about code...

WP7 - Data Between Pages – Part 1

WP7 - Data Between Pages – Part 1

In my last post about WP7 I talked about The Mask Way– Rating Control and More, but before I’ll jump to more cool stuff let’s start from the basics.

  • How to transfer data between pages?
  • How to shared data for the entire WP7 application?
  • And of course how to retaining data across instances?

Download Demo Project

image

This is the first post of two about Passing Data Between Pages in WP7.

Query String

Query string is the part of a Uniform Resource Locator (URL) that contains data to be passed to web applications.

Passing Data using WP7 Query String Format it’s that same as HTML Query String.

I’ve created a Const class to obtain my strings:

public class Const
{     
public static string TextTag = "Text";
    public static string BoldTag = "IsBold"; }
image
Source Page:
Using NavigationService to navigate to Destination Page with our Query String – /Page.Xaml?Param1=Value1&Param2=Value2
private void BtnQueryDemoClick(object sender, RoutedEventArgs e)
{     
var queryData = string.Format("?Text={0}&IsBold={1}", txtQueryData.Text, chxIsBold.IsChecked); 
    NavigationService.Navigate(new Uri("/QueryData.xaml" + queryData, UriKind.Relative)); }

Destination Page:

In order to receive those parameters you need to override the OnNavigatedTo method and use the NavigationContext to obtain the QueryString.

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    var data = this.NavigationContext.QueryString;

    if (data.ContainsKey(Const.TextTag))
        txtMain.Text = data[Const.TextTag];

    if (data.ContainsKey(Const.BoldTag))
        txtMain.FontWeight = Convert.ToBoolean(data[Const.BoldTag]) ? FontWeights.Bold : FontWeights.Normal;

    base.OnNavigatedTo(e);
}

 

Static Data

Always remember that each and every page in your WP7 application has convenient access to App class that derives from Application and have static property called – Application.Current.

So you can Cast this App class from any page and use it to store and get data.

For Example I’ve created one public property – SharedText (string)

public partial class App : Application
{     
public string SharedText { get; set; } 
            public App()  
   {        
SharedText = "Hello World!!!!";
In my Test Page I’ve override the OnNavigatedTo and cast an App from Application.Current.
Using the App I can get my Static Property and set the value to the TextBox (txtMain)
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{     
var app = (Application.Current as App);   
txtMain.Text = app.SharedText;     
            base.OnNavigatedTo(e); }
image
Now, when I click the “Set Data & Move”, I’m using the same concept but instead of Get the property I’m setting a new value.

private void BtnMoveClick(object sender, RoutedEventArgs e)
{
      var app = (Application.Current as App);
      app.SharedText = txtMain.Text;

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

In the Destination Page you will see:

image

And the Code behind is the same:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{     
var app = (Application.Current as App);  
   txtMain.Text = app.SharedText;    

base.OnNavigatedTo(e); }
In the next post I’ll talk about Isolated Storage and Phone States.
Download Demo Project
Enjoy.

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: