This chapter is taken from book "Programming Windows Phone 7" by Charles Petzold published by Microsoft press. http://www.charlespetzold.com/phone/index.html
Page navigation would seem to be an advanced Silverlight programming topic, and a topic that applies only to Silverlight programming rather than XNA programming. However, there are issues involved with navigation that are related to the very important topic of tombstoning, which is what happens to your Windows Phone 7 application when the user navigates to another application through the phone's Start screen. Tombstoning is very much an issue that also affects XNA programmers.
Basic Navigation
The SilverlightSimpleNavigation project begins as usual with a
MainPage class, and as
usual I set the two TextBlock
elements for the titles and the content area of MainPage.xaml contains
only a TextBlock
that
sets a handler for its ManipulationStarted
event:
<StackPanel
x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock
x:Name="ApplicationTitle"
Text="SIMPLE NAVIGATION"
Style="{StaticResource
PhoneTextNormalStyle}"/>
<TextBlock
x:Name="PageTitle"
Text="main page"
Margin="9,-7,0,0"
Style="{StaticResource
PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place
additional content here-->
<Grid
x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<TextBlock
Text="Navigate to 2nd
Page"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0 34"
ManipulationStarted="OnTextBlockManipulationStarted"
/>
</Grid>
Notice the Text
property on the TextBlock:
"Navigate to 2nd page." The code-behind file contains the handler for
ManipulationStarted but
also overrides the OnManipulationStarted
method for the whole page:
namespace
SilverlightSimpleNavigation
{
public partial
class MainPage
: PhoneApplicationPage
{
Random rand =
new Random();
public MainPage()
{
InitializeComponent();
}
void OnTextBlockManipulationStarted(object
sender, ManipulationStartedEventArgs args)
{
this.NavigationService.Navigate(new
Uri("/SecondPage.xaml",
UriKind.Relative));
args.Complete();
args.Handled = true;
}
protected
override void OnManipulationStarted(ManipulationStartedEventArgs
args)
{
ContentPanel.Background = new
SolidColorBrush(
Color.FromArgb(255, (byte)rand.Next(255),
(byte)rand.Next(255),
(byte)rand.Next(255)));
base.OnManipulationStarted(args);
}
}
}
If you touch anywhere on the page outside of the TextBlock, the background of the ContentPanel is set to a random color. Touch the TextBlock, and the handler accesses the NavigationService property of the page.
I created a second page in the SilverlightSimpleNavigation project by right-clicking the project name in the Visual Studio solution explorer, and selecting Add and New Item. From the Add New Item dialog box, I picked Windows Phone Portrait Page and gave it a name of SecondPage.xaml.
I gave the titles In SecondPage.xaml the same application name as
FirstPage.xaml but a page title of "second page" and the content area of
SecondPage.xaml is very much like MainPage.xaml but the
TextBlock reads "Go
Back to 1st Page":
<StackPanel
x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock
x:Name="ApplicationTitle"
Text="SIMPLE NAVIGATION"
Style="{StaticResource
PhoneTextNormalStyle}"/>
<TextBlock
x:Name="PageTitle"
Text="second page"
Margin="9,-7,0,0"
Style="{StaticResource
PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid
x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<TextBlock
Text="Go Back to 1st Page"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0 34"
ManipulationStarted="OnTextBlockManipulationStarted"
/>
</Grid>
The code-behind file of the
SecondPage class is also very much like the
FirstPage class:
namespace
SilverlightSimpleNavigation
{
public partial
class SecondPage
: PhoneApplicationPage
{
Random rand =
new Random();
public SecondPage()
{
InitializeComponent();
}
void OnTextBlockManipulationStarted(object
sender, ManipulationStartedEventArgs args)
{
this.NavigationService.GoBack();
args.Complete();
args.Handled = true;
}
protected
override void OnManipulationStarted(ManipulationStartedEventArgs
args)
{
ContentPanel.Background = new
SolidColorBrush(
Color.FromArgb(255, (byte)rand.Next(255),
(byte)rand.Next(255),
(byte)rand.Next(255)));
base.OnManipulationStarted(args);
}
}
}
Let's run the program. The program begins with the main page, and you can touch the screen to change the color:

Now touch the TextBlock that says "Navigate to 2nd Page" and the second page comes into view:

You can touch that screen to change to a different color:

Now touch the TextBlock that says "Go Back to 1st Page". (Alternatively, you can press the phone's hardware Back button.) You'll be whisked back to the main page with the color just as you left it:

Passing Data to Pages
The possible use of pages as dialog boxes provokes two questions:
- How do I pass data from a source page to a destination page?
- How do I return data when going back to the original page?

Join the conversation! Your thoughts help the community grow.