Recently, I was a beginner in Silverlight and had a lot of problems to solve. I wanted to translate all the good English versions here to help beginners who are not good at English (although I am not good at English). At the same time, the translation process is also a learning process. Time is tight. This time, we will record the method for switching the page.
This is the video "using multiple pages, Part 1" from "Jesse liberty". The basic method is to create a pageswticher. This pageswitcher does not directly display the page, but serves as a background, switches pages.
The specific method is:
Create a new usercontrol named pageswitcher. Remove the Grid Control in pageswitcher. XAML. The program is as follows:
< Usercontrol X: Class = "Silverlightdemo. pageswitcher"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
Xmlns: x =" Http://schemas.microsoft.com/winfx/2006/xaml%22 >
</ Usercontrol >
Then in the backgroundCodeAdd a method, for example, switchpage, to switch pages, and then call it in the constructor to display the first page to be displayed:
Public Partial Class Pageswitcher: usercontrol
{
Public Pageswitcher ()
{
Initializecomponent ();
Switchpage ( New Page ()); // Display the first page to be displayed
}
/// <Summary>
/// Switch page
/// </Summary>
/// <Param name = "Newpage"> Page to be switched </Param>
Public Void Switchpage (usercontrol Newpage)
{
This . Content = Newpage;
}
}
Then, in the Event Response Method to switch pages on each page, you only need to do this:
Private Void Btn_click ( Object Sender, routedeventargs E)
{
Pageswitcher switcher = This . Parent As Pageswitcher;
Switcher. switchpage ( New Anotherpage ());
}
Finally, modify the application_startup method in APP. XAML. CS to modify the start page.
Private Void Application_startup ( Object Sender, startupeventargs E)
{
This . Rootvisual = New Pageswitcher ();
}