Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Navigation Framework in Silverlight 4

0.00/5 (No votes)
12 Apr 2011 1  
Navigation framework in Silverlight 4

Navigation is an important and one of the basic aspects of web application. In earlier versions of Silverlight, Navigation system was missing, until Silverlight 3. This blog post is all about the concept of Navigation Framework and an attempt to summarize the overall concept. [Check online Demo of this post].

Basic Concept

Frame

  • The concept of Navigation system revolves around Frame. Frame Control acts as a container for pages, it validates the state and maintains the navigation history.
  • A Frame can host one page at a particular time.

Manas Patnaik Blog

Source: Source property of Frame defines the default page to load when the application is initialized.
Navigate (URI uri): Navigate method navigates to the specific URI from code.

<sdk:Frame Margin="0,37,0,-15" 

Name="frameContainer" 

Source="/View/Home.xaml"/>

Page

  • Pages are the superset of controls which are capable of Navigation and allow itself to load inside a frame.
  • It is important to know the relation between Frame and Page.The above figure illustrates the relation between them. Frame is responsible for navigating to a particular page with the URI. Once page has been loaded, it can access the host Navigation system by NavigationContext and NavigationService.

Step By Step with a Example

Create a Silverlight Application

  • Let's create a Silverlight Application from Visual Studio. As shown in the figure, we have added some pages to the Silverlight project.

    Manas Patnaik Blog

  • The MainPage will load the content pages (In View Folder) with a click of navigation Hyperlinks.

    Manas Patnaik Blog

Add Event Handler to Navigate

For the project, XAML code of the MainPage is as follows:

<Grid x:Name="LayoutRoot" Background="White"> 
<sdk:Frame Margin="0,37,0,-15" 

Name="frameContainer" 

Source="/View/Home.xaml"/> 

<HyperlinkButton Content="Home" 

Height="19" HorizontalAlignment="Left" 

Margin="12,12,0,0" Name="hlHome" 

VerticalAlignment="Top" Width="39" Click="hlHome_Click" /> 
<HyperlinkButton Content="About" 

Height="19" HorizontalAlignment="Left" 

Margin="57,12,0,0" Name="hlAbout" 

VerticalAlignment="Top" Width="35" /> 
<HyperlinkButton Content="Customer" 

Height="19" HorizontalAlignment="Left" 

Margin="98,12,0,0" Name="hlCustomer" 

VerticalAlignment="Top" Width="62" /> 
</Grid>

Notice the HyperlinkButtonHome” click Event. It calls the code behind logic of navigating to the particular page.

private void hlHome_Click(object sender, RoutedEventArgs e) 
{ 
this.frameContainer.Navigate(new Uri("/View/Home.xaml", UriKind.Relative )); 
}

Add Error Page for Resource Not Found

The frame can handle the following events while navigating to a page:

<sdk:Frame Margin="0,37,0,-15" 
Name="frameContainer" 
Source="/View/Home.xaml" 
NavigationFailed="frameContainer_NavigationFailed" /> 
private void frameContainer_NavigationFailed(object sender, 
    System.Windows.Navigation.NavigationFailedEventArgs e) 
{ 
e.Handled = true; 
frameContainer.Navigate(new Uri("/View/ErrorPage.xaml", UriKind.Relative)); 
}

Navigation Through XAML

Instead of the above coding approach, we can specify Hyperlink NavigateUri property to their relative URIs.

<HyperlinkButton Content="About" 

Height="19" HorizontalAlignment="Left" 

Margin="57,12,0,0" Name="hlAbout" 

VerticalAlignment="Top" Width="35" 

NavigateUri="/View/About.xaml" 

TargetName="frameContainer" 

/>

Hiding Resources Through URI Mapper

Sometimes, it is necessary to hide your resource location also the long URL for the particular page is not so user-friendly. So we can use URIMAPPER to enable with short and meaningful names for URIs. Suppose for this example, the user should be allowed to navigate the Customers page with adding “/Customers” to the URL. To use URIMAPPING, we need the following 2 references to our mainpage.xaml page:

xmlns:navigation=
    "clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
xmlns:uriMapper=
    "clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
  • Our next step will be adding mapping to the Frame Control.
    <sdk:Frame Margin="0,37,0,-15" 
    
    Name="frameContainer" 
    
    Source="/View/Home.xaml" 
    
    NavigationFailed="frameContainer_NavigationFailed" > 
    
    <navigation:Frame.UriMapper> 
    <uriMapper:UriMapper> 
    <uriMapper:UriMapping Uri="/Customers" 
    
    MappedUri="/View/Customers.xaml" /> 
    </uriMapper:UriMapper> 
    </navigation:Frame.UriMapper> 
    
    </sdk:Frame>

    Now we need to set the Hyperlink navigateURI property as /”Customers “. So this will ensure that the “/Customer” is mapped to the mentioned MapeedUri.

    <HyperlinkButton Content="Customer" 
    
    Height="19″ HorizontalAlignment="Left" 
    
    Margin="98,12,0,0″ Name="hlCustomer" 
    
    VerticalAlignment="Top" Width="62″ 
    NavigateUri="/Customers" 
    
    />

Now, Customers page can be accessible by the new User friendly URI. “http://localhost:6469/NavigationSystemTestPage.aspx#/Customers”

Accessing Navigation System from a Page

  • As we mentioned above, a page can access navigation system using NavigationService. In this example, we have a DashBoard page which required to be loaded once the user clicks on the button in the customers page. This can be achieved through code behind in Customers page on button click event.
    private void btnShowDashBoard_Click(object sender, RoutedEventArgs e) 
    { 
    NavigationService.Navigate(new Uri("/Admin/DashBoard.xaml", UriKind.Relative)); 
    }

    NavigationService allows the page to get the host navigation service that is used to navigate to this page.

    Manas Patnaik Blog

Final Words

Silverlight navigation supports much more complex navigation logic such as Fragmented Navigation, Content loader, etc. This post only touches the basics of navigation. More posts will follow soon about navigation and Silverlight.

You can download the source code here.

You can take a look at the online demo here.

Links and References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here