In a slightly more complex application, multiple pages may be used. To jump to these pages, you must use the navigation function.
The following example provides a basic function: navigate from mainpage on the home page to secondpage, and then return to the mainpage from secondpage.
Mainpage XAML code:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Navigate to Second Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>
Mainpage C # code:
Public partial class mainpage: phoneapplicationpage
{
// Constructor
Public mainpage ()
{
Initializecomponent ();
}
Private void textblock_manipulationstarted (Object sender, manipulationstartedeventargs e) // process the manipulationstarted event
{
// Todo: Jump to the secondpage page when you click textblock.
This. navigationservice. navigate (New uri ("/secondpage. XAML", urikind. Relative); // call the navigate () method of the navigationservice class to implement navigation
E. Complete (); // indicates that other manipulation events will not be processed.
E. Handled = true; // indicates that the route event has been handled and does not need to be passed up.
}
}
Secondpage XAML code:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Go back to Main Page"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>
Secondpage C # code:
Public partial class secondpage: phoneapplicationpage
{
Public secondpage ()
{
Initializecomponent ();
}
Private void textblock_manipulationstarted (Object sender, manipulationstartedeventargs e) // process the manipulationstarted event
{
// Todo: return to the main page
This. navigationservice. Goback (); // call the Goback () method of the navigationservice class to return to the previous page, which is equivalent to the next line of code.
// This. navigationservice. navigate (New uri ("/mainpage. XAML ", urikind. relative); // The difference is that the program will not return to the original mainpage instance, but will navigate to a new mainpage instance.
E. Complete ();
E. Handled = true;
}
}
Effect
Click textblock and navigate to the secondpage. Click textblock to return to the mainpage.
Note: 1. The manipulationstarted event occurs when the operation is performed on the uielement object (textblock here. Click it to happen immediately.
2. The navigationservice attribute is a read-only attribute defined in the page class. It is of the navigationservice type, and the navigationservice class is a seal class. We cannot instantiate it, create their own navigationservice instances by the host type. The navigationservice class includes the navigate () method and Goback () method.
3. Pay attention to this. navigationservice. goback () and this. navigationservice. navigate (New uri ("/mainpage. XAML ", urikind. relative), although they achieve the same effect, but their principles are different.
The next article will summarize the data transmission between pages in the Silverlight application.