Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » XAML » URI Mappings in Silverlight Navigation Framework

URI Mappings in Silverlight Navigation Framework

URI Mappings in Silverlight Navigation Framework

Author Rank :
Page Views : 8963
Downloads : 121
Rating :
 Rate it
Level : Advanced
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
NavigationFrameworkSample.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Introduction

Navigation Framework comes with Silverlight 3 RIA Services. Using this framework we can easily navigate between User Controls in a Silverlight Application. We can enable the Browser History capabilities and URI mappings can also be done.

To start with, we will build a simple Silverlight Application which will be easy to catch up.

Create Silverlight Navigation Application

Create a Navigation Application and name it NavigationFrameworkSample. The following screenshots will guide you through the process.

Figure1.gif

Figure 1.1 Creating a Silverlight Navigation Application

Figure2.gif

Figure 1.2 Enabling the RIA Features by Linking the web page

Figure3.gif

Figure 1.3 Solution explorer

Here MainPage.xaml will work as our master page, which will carry the common elements for the whole projects. Now we will redesign the template to our simple use. It will look like the following. Using Blend 3 I have tried to change the template a bit; you can explore your own creativity.

Figure4.gif

Figure 1.4 Changed MainPage.xaml in Blend 3

Xaml Code

<UserControl x:Class="NavigationFrameworkSample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
                Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="LightBlue">
                <Grid.RowDefinitions>
                                <RowDefinition Height="0.107*"/>
                                <RowDefinition Height="0.893*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="0.26*"/>
                                <ColumnDefinition Width="0.74*"/>
                </Grid.ColumnDefinitions>
                                <Border  Grid.Row="1" Style="{StaticResource FrameInnerBorderStyle}">
                <StackPanel >
                                <Button Click="NavButton_Click" Tag="/Views/HomePage.xaml" Content="Home"
                            Style="{StaticResource PageLinkStyle}"/>
            <Button Click="NavButton_Click" Tag="/Views/AboutPage.xaml" Content="About"
                            Style="{StaticResource PageLinkStyle}"/>
                                </StackPanel>
                                </Border>
                <Border  Grid.Column="1" Grid.Row="1" BorderBrush="#FF000000" Style="{StaticResource FrameContainerStyle}" Margin="0,0,0,-4">
                                <navigation:Frame x:Name="Frame" Source="/Views/HomePage.xaml"
                                  HorizontalContentAlignment="Stretch"
                                  VerticalContentAlignment="Stretch"
                                  Padding="15,10,15,10"
                                  Background="White"/>
                                </Border>
                <TextBlock Grid.Column="1" FontSize="22" VerticalAlignment="Center" HorizontalAlignment="Center" Text="Navigation Framework" TextWrapping="Wrap"/>
    </Grid>
</
UserControl>

Navigation Page

Navigation Framework has its own Page that is called navigation Page. Which we will see further. Navigation Framework's Views Folder consists of the Xaml Pages that are related to the project. In this sample application we have two Xaml Pages, Home.xaml and About.Xaml.

Figure5.gif

Figure 1.5 Other Pages in Views Folder

Xaml Code for Home Page

<navigation:Page x:Class="NavigationFrameworkSample.HomePage"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           Title="HomePage ">
    <Grid x:Name="LayoutRoot" Background="White">
                <StackPanel>
                   
<TextBlock Text="Home" FontSize="20"/>
                    <StackPanel>
                        <TextBlock Text="Navigation Framework in Silverlight 3" />
                    </StackPanel>
        </StackPanel>
    </Grid>
</navigation:Page>

Xaml Code for About Page

<navigation:Page x:Class="NavigationFrameworkSample.AboutPage"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           Title="AboutPage ">
    <Grid x:Name="LayoutRoot" Background="White">
                <StackPanel>
            <TextBlock Text="About" FontSize="20"/>
            <TextBlock Text="Navigation Framework is all about Navigating between User Controls"/>
        </StackPanel>
    </Grid>
</navigation:Page>

As soon as we create the Navigation Application, System.Windows.Controls.Navigation.DLL is added to the project's reference.
In Navigation framework we have a control called Frame, which can load a Page into this. It has the same concept as Frame in ASP.NET.

To navigate through the pages we will use the following default logic of the framework.

private void NavButton_Click(object sender, RoutedEventArgs e)
        {
            Button navigationButton = sender as Button;
            String goToPage = navigationButton.Tag.ToString();
            this.Frame.Navigate(new Uri(goToPage, UriKind.Relative));
        }

URI Mapping

This concept is new in Silverlight 3 Navigation Framework. Using this concept we can hide our Xaml page's location and we can achieve security.
To achieve the above said, we need to do a trick in our App.xaml. We need to add the System.Windows.Controls.Navigation.dll to the xaml and change as follows: Change the default namespace.

Before

xmlns:navigationCore="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"

After

xmlns:navigationCore="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"

Now we are enabled to write the URI Mappings. Write the following as per your requirement.

<navigationCore:UriMapper x:Key="uriMapper">
            <navigationCore:UriMapping Uri="Home" MappedUri="/Views/HomePage.xaml"/>
            <navigationCore:UriMapping Uri="About" MappedUri="/Views/AboutPage.xaml"/>
</navigationCore:UriMapper>

Change the Following tag in Button

<Button Click="NavButton_Click" Tag="Home" Content="Home"  Style="{StaticResource PageLinkStyle}"/>
 <Button Click="NavButton_Click" Tag="About" Content="About" Style="{StaticResource PageLinkStyle}"/>

Now we can change the path to our Uri anywhere in the project, like we used it in our startup page in MainPage.xaml. We can now change the source attribute of the Frame.

Source="Home"

Now we are ready to test our Sample and using this framework we enabled our browser history capability by default, that means now we can do Back and Forward for our required destination pages. The following screenshots will guide you through.

Figure6.gif

Figure 1.6 Running the Application

Figure7.gif

Figure 1.7 Typing the URI after # and getting the page

Figure8.gif

Figure 1.8 Browser Histories in IE.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Diptimaya Patra

Diptimaya is working as a Sr. Software Engineer in Microsoft Technologies (C#). He is a Microsoft MVP in Client App Dev, he has a good hands on in Silverlight 2/3/4, WPF 3/4, Expression Blend 3/4.


Follow him in Twitter: http://www.twitter.com/dpatra

Blog: http://dpatra.blogspot.com , http://diptimayapatra.wordpress.com

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Navigation uri Error occured by shahbaz On July 31, 2009
Please help me out its urgent when i run this sample on my machine it give me runtime script error that "Navigation is only supported to relative URIs that are fargment, or begin with '/', or which contain, component/"
Reply | Email | Modify 
Re: Navigation uri Error occured by Diptimaya On July 31, 2009

Hi,

The sample provided for this article was built with Silverlight 3 Beta. If you create a new Silverlight 3 Navigation Application and follow the steps it will solve youe issue.

Try it, revert me if problem persists.

Thanks

Diptimaya Patra

Reply | Email | Modify 
Nice one.. by Aravind On December 6, 2011
I was searching article for uri mapping.. and its so nice.. thanks.
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.