There are two ways to implement navigational applications in silverlight:
private static Dictionary<Pages, UserControl> pageCache =new Dictionary<Pages, UserControl>();
public static void Navigate(Pages newPage) { // Get the current application object and cast it to // an instance of the custom (derived) App class. App currentApp = (App)Application.Current; // Check if the page has been created before. if (!pageCache.ContainsKey(newPage)) { // Create the first instance of the page, // and cache it for future use. Assembly assembly = type.Assembly; pageCache[newPage] = (UserControl)assembly.CreateInstance( type.Namespace + "." + newPage.ToString()); } // Change the currently displayed page. currentApp.rootGrid.Children.Clear(); currentApp.rootGrid.Children.Add(pageCache[newPage]); } The limitation with this approach that the browser has no idea of the current silverlight page being displayed , the browser is aware of only the HTML or aspx page hosting the control. Other disadvantage with this approach is that if we want to navigate through a long sequence of pages this manual approach is cumbersome. If we want to add support for browser navigation we need to use Silverlights navigation system through the Page and Frame classes which are available in silverlight 3.0 Both of these classes provides navigation features of the silverlight navigation system. Implementing Navigation by using Frames Frame control is content control meaning it provides content property that points to the single child that the control can contain.But we use the navigate() method instead which apart from changing the content property also updates the browsers page history and updates the browsers URI. To use the Frame class first we need to map the xml namespace to the System.Windows.Controls namespace xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Now we declare the Frame class in xaml with the name mainframe which can use in the code to navigate to the different page. <navigation:Frame x:Name="mainFrame"></navigation:Frame> In the button click event handlers we can use code like below: mainFrame.Navigate(new Uri("/About.xaml", UriKind.Relative)); If our silverlight application is hosted in TestPage.html ,this will display URI in the browser like this : localhost://Navigation/TestPage.html#/About.xaml If we bookmark the above URL and return back then we will get the About.xaml control loaded in the page . This feature is called deep linking. In the next article we will see how to implement navigation by using Page class.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: