Reader Level:
ARTICLE

Simple Navigation in silverlight

Posted by Mahadesh Mahalingappa Articles | Silverlight with C# August 18, 2011
In this article we are going to see how we can navigate between pages without using the Navigation Framework.
  • 0
  • 0
  • 6709
Download Files:
 

Navigation Framework is really good but in some cases we do not want to use the Navigation Framework. This navigation technique can be used instead which provides simple Navigation.
 
Add the following code in App.xaml:
 
 
        private static Grid root;       
         public static void Navigate(UserControl newPage)
         {
             UserControl oldPage = root.Children[0] as UserControl;
             root.Children.Add(newPage);
             root.Children.Remove(oldPage);
         }
 

Modify the App.xaml as shown below:
 
Previous code:
 
        
private void Application_Startup(object sender, StartupEventArgs e)
         {
             this.RootVisual = new MainPage();
         }

 
Modified code:
 
 
        private void Application_Startup(object sender, StartupEventArgs e)
         {
             root = new Grid();
             root.Children.Add(new MainPage());
             this.RootVisual = root;     
         }

 
Create a new usercontrol NewPage . Add Button to the Page inorder to navigate to the Home. Similarly create a button in the MainPage.xaml as well inorder to navigate to the NewPage.
 
In the Button Click event add the following code ;
 
       
App app = (App)Application.Current;
        App.Navigate(new NewPage());

 
Application.Current gets the System.Windows.Application object for the current application.
 
The new instance of the Page is passed to the Navigate method of App class.
 
When we run the code we are able to navigate between the MainPage and the NewPage.xaml.

COMMENT USING

Trending up