Tuesday 10 January 2012

Navigation In Silverlight

Navigation in Silverlight can be achieved by managing Root Visual in App.xaml. We can use layout container as Silverlight application's root visual, this container may be border or Grid. In root visual, we can change user control that represents first page with different user control that represents other page. This technique is simple and requires less codes. It also has ability of state management and animation.

//App.xaml.cs
   
       // This Grid will host pages.
        private Grid rootGrid = new Grid();

        private void Application_Startup(object sender, StartupEventArgs e)
        {
           // Load the first page.
            this.RootVisual = rootGrid;
            rootGrid.Children.Add(new Register());
        }

        public static void Navigate(UserControl newPage)
        {
            App currentApp = (App)Application.Current;
            // Change the currently displayed page.
            currentApp.rootGrid.Children.Clear();
            currentApp.rootGrid.Children.Add(newPage);
        }

Note : Navigate method is static for making process straightforward.

Now, In Silverlight application navigation can be performed by using this code :)

App.Navigate(new confirm());

No comments:

Post a Comment