Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Windows Phone 7 Execution Model : Navigating Between Pages by Using Global Application Variables

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/6/2011 11:42:53 AM

1. Problem

You have to navigate between pages sharing data across them. You want to avoid using a query string because your application has a lot of pages, and you need a smart way to share data between pages.

2. Solution

You can add a public property to the App class and retrieve its value by using the Current static property provided by the Application class.

3. How It Works

The Windows Phone 7 Silverlight application always provides a class deriving from the Application class: the App class. This class represents the running application itself with a reference to the root frame and pages.

Defining a property in the App class ensures that the property is visible to all pages. At any time and from any page, you can use the Current static property from the Application class to retrieve a reference to the current App object. By using the instance of this object, you can easily either retrieve the property value or set it.

4. The Code

Create a new Windows Phone 7 Silverlight application called NavigatingWithGlobalVariableApp from Visual Studio 2010. Add two new pages called Page2.xaml and Page3.xaml by choosing Project Add New Item.

In the App.xaml.cs file, we added a Boolean property that represents the user's choice to have all red pages in the application.

public partial class App : Application
{
/// <summary>
/// Provides easy access to the root frame of the Phone Application.
/// </summary>
/// <returns>The root frame of the Phone Application.</returns>
public PhoneApplicationFrame RootFrame { get; private set; }

public bool RedPages { get; set; }


/// <summary>
/// Constructor for the Application object.
/// </summary>
public App()
{
. . .

In the MainPage.xaml file, we added a check box and two hyperlink buttons. The check box has two event handlers to manage selected and deselected states. When the check box is selected, the background of every page will be red; otherwise, it will be black.

. . .
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<CheckBox x:Name="chkRedColor" Content="I want all red pages"
Checked="chkRedColor_Checked" Unchecked="chkRedColor_Unchecked" />
<HyperlinkButton Content="Navigate to page2" x:Name="hbToPage2"
Click="hbToPage2_Click" />
<HyperlinkButton Content="Navigate to page3" x:Name="hbToPage3"
Click="hbToPage3_Click" />
</StackPanel>
</Grid>
. . .


In the Checked and Unchecked event handlers' code, we set the RedPages Boolean to true and false, respectively. Moreover, we change the background color properly.

private void chkRedColor_Checked(object sender, RoutedEventArgs e)
{
App a = Application.Current as App;
a.RedPages = true;
ContentPanel.Background = new SolidColorBrush(Colors.Red);
}

private void chkRedColor_Unchecked(object sender, RoutedEventArgs e)
{
App a = Application.Current as App;
a.RedPages = false;
ContentPanel.Background = new SolidColorBrush(Colors.Black);
}


As usual, both hyperlink buttons' event handlers contain the code to navigate to the other pages.

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


private void hbToPage3_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("/Page3.xaml", UriKind.Relative));
}


In Page2.xaml and Page3.xaml, we added the Loaded event handler. In the related code, we retrieved the RedPages Boolean property, setting the page's background color accordingly.

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
App a = Application.Current as App;
if (a.RedPages == true)
ContentPanel.Background = new SolidColorBrush(Colors.Red);
}

5. Usage

Run the application from Visual Studio 2010, selecting Windows Phone 7 Emulator as the output target. From the main page, select the I Want All Pages Red check box. The background color turns red (see Figure 1).

Figure 1. NavigatingWithGlobalVariableApp in action

Click the check box again to deselect it, and the main page's background color turns black. Select the check box once more and then press the Navigate to page2 hyperlink button. The Page2 background color is red (see Figure 2).

Figure 2. Evenpage 2 has the background color set to red.
Other -----------------
- Windows Phone 7 Execution Model : Passing Data Through Pages
- Windows Phone 7 Execution Model : Navigating Between Pages
- Developing for Windows Phone 7 and Xbox 360 : Using the Content Pipeline - Content Importers
- Developing for Windows Phone 7 and Xbox 360 : Using the Content Pipeline - Content Processors
- Developing for Windows Phone 7 and Xbox 360 : Introduction to Custom Effects - Effect States
- Creating a Trial Windows Phone 7 Application
- Deploying the Windows Phone 7 Application on the Device
- Deploying the Application to the Windows Phone 7 Emulator
- Creating a Simple XNA Windows Phone 7 Application
- Creating a Simple Silverlight Windows Phone 7 Application
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server