Friday, March 27, 2009

Silverlight 3 Beta Navigation Application support

This is another new feature added in Silverlight 3 which is based on WPF paging concept.Navigation or Paging support gives us flexibility to design our application as page wise.It is more likely to ASP.Net page concept than windows programming.There will be a container which contains pages and we can switch among pages by user actions as well as by programmatically.The main advantage of this is we can identify each Silverlight page by it’s url.We will discuss about this deep linking feature in a new post.

Creating Silverlight navigation application / paging enabled Application.

The Silverlight 3 tools for visual studio 2008 brings a new project template for Navigation application.Select that project template and create.The project template itself contains 3 sample Pages AboutPage.xaml,ErrorWindow.xaml and HomePage.xaml in the Views folder.

These pages are hosted in MainPage.xaml using a control called Frame coming from the assembly System.Windows.Controls.Navigation.The Source property of Frame denotes the current page which is displaying in Frame.Change the Source property if you need to change the current page.
When we run the application we can see a good style for the controls in it.That is not magic of paging but the effect of styles and templates present in App.xaml :-)

Switching among pages

There are 2 methods to switch current page.

this.Frame.Navigate(new Uri(“goToPageURI”, UriKind.Relative));
this.Frame.Source = new Uri(“goToPageURI”, UriKind.Relative);

Controlling navigation / paging behavior

There are so many properties and methods available in Page and Frame classes to control the navigation behavior such as Frame.CanGoBack,Frame.CanGoForward,Frame.StopLoading() etc….Main requirement will be getting the query string parameters in the page in order to load something or show something.It is available in NavigationContext.QueryString property.

Note that the query string contains the name value pairs which comes after the ‘;’ symbol.You can’t get the path (Eg:#/Views/HomePage.xaml)to page there.

NB :The above post is written based on Silverlight 3 Beta build.Things may change in the actual release.

No comments: