Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing - DevCorner

Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing

Silverlight 3 introduced the Navigation Framework and URI Routing features. Today I’ll show how to use those features.

To use navigation features you could either create a new “Silverlight Navigation Application” or simply add relevant references to standard “Silverlight application”

image

In case of standard “Silverlight Application” you need to add reference to “System.Windows.Controls.Navigation” assembly and relevant XAML introduce namespace:

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

To use the navigation feature, simply add new Frame to your XAML page. Frame could have default content, specify Journal usage (own history storage or integration within browser’s history), raise navigation related events.

<navigation:Frame x:Name="frame" Source="/Pages/DefaultPage.xaml"
                      HorizontalContentAlignment="Stretch"
                      VerticalContentAlignment="Stretch"
                      Padding="15,10,15,10"
                      Margin="5,0"
                      Background="White">
  <TextBlock Text="This is default content for the navigation frame" FontFamily="Times New Roman" FontSize="25"/>
</navigation:Frame>

To navigate to new page use “Navigate” function of Frame object:

frame.Navigate(new Uri(“/Pages/Page1.xaml”, UriKind.Relative));

Navigation could be done to class instances derived from “System.Windows.Controls.Page” class. The simplest way to create such a class (and XAML) is by adding to the project new item from “Silverlight Page” type:

image

In addition to navigation, the Frame provides functionality to navigate Forward/Backward according to own/browsing history. Sample “Next”/“Previous” button handlers:

private void btnNext_Click(object sender, RoutedEventArgs e)
{
  if (frame.CanGoForward)
    frame.GoForward();
}

private void btnPrev_Click(object sender, RoutedEventArgs e)
{
  if (frame.CanGoBack)
    frame.GoBack();
}

The history definition for the frame defined by JournalOwnership dependency property:

image

Automatic [default] Whether or not this Frame will create and use its own journal depends on its parent
OwnsJournal The Frame maintains its own journal
UsesParentJournal The Frame uses the journal of the next available navigation host up the content tree, if available. Otherwise, navigation history is not maintained for the Frame

 

To raise navigation related events subscribe to themNext:

//This event will be fired when frame is already navigated
this.frame.Navigated += new NavigatedEventHandler(frame_Navigated);
//This event will be fired when frame is navigating
this.frame.Navigating += new NavigatingCancelEventHandler(frame_Navigating);
//This event will be fired when navigation error occurs
this.frame.NavigationFailed += new NavigationFailedEventHandler(frame_NavigationFailed);

By using this Navigation Framework features you could implement deep linking support by URI Routing feature. To use this feature you need to create the UriMapper in application resources. To do so first you need to introduce a namespace in your App.XAML:

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

Then create the UriMapper as a part of application resources:

<Application.Resources>
  <navcore:UriMapper x:Key="uriMapper">
    <navcore:UriMapping Uri="Home" MappedUri="/Pages/DefaultPage.xaml"/>
    <navcore:UriMapping Uri="Link{number}" MappedUri="/Pages/Page{number}.xaml"/>
    <navcore:UriMapping Uri="Page/{number}" MappedUri="/Pages/Page{number}.xaml"/>
    <navcore:UriMapping Uri="Customers/{id}" MappedUri="/Pages/Page1.xaml?action=getCustomer&amp;id={id}"/>
    <navcore:UriMapping Uri="Products/{id}" MappedUri="/Pages/Page1.xaml?action=getProduct&amp;id={id}"/>
    <navcore:UriMapping Uri="Stories/{id}&amp;{number}" MappedUri="/Pages/Page1.xaml?action={id}&amp;page={number}"/>
  </navcore:UriMapper>
</Application.Resources>

In Beta 1 build the Uri Mapper must have “uriMapper” name, otherwise it will not work.

In my case the mapping is very straightforward:

Uri Target + Description
Home /Pages/DefaultPage.xaml
Link{number} /Pages/Page{number}.xaml –-> use {number} as variable received from navigation input
Page/{number} /Pages/Page{number}.xaml –-> use {number} as variable received from navigation input
Customers/{id} /Pages/Page1.xaml?action=getCustomer&amp;id={id} –-> use {id} as part of Query String
Products/{id} /Pages/Page1.xaml?action=getProduct&amp;id={id} –-> use {id} as part of Query String
Stories/{id}&amp;{number} /Pages/Page1.xaml?action={id}&amp;page={number} –> use {id} and {number} in Query String. You could introduce as many, as you need.

When navigating, the navigating URI becomes something much more readable (and SEO):

image

image

image

image

The passed query string parameters could be used on specific page from NavigationContext. While creating new “Silverlight Page” visual studio also creates override function “OnNavigatedTo” which being executed right after navigation to the page:

// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
  if (this.NavigationContext.QueryString.ContainsKey("action"))
  {

    switch (this.NavigationContext.QueryString["action"])
    {
      case "getCustomer":
        txtOutput.Text = "This is DUMMY info CustomerID = " + this.NavigationContext.QueryString["id"];
        break;
      case "getProduct":
        txtOutput.Text = "This is DUMMY info ProductID = " + this.NavigationContext.QueryString["id"];
        break;
      default:
        txtOutput.Text = "This is DUMMY story saved in my sources with ID = " 
          + this.NavigationContext.QueryString["action"] + " opened on page #" 
          + this.NavigationContext.QueryString["page"];
        break;
    }
  }
}

That’s it for today.

Sample application sources is here.

 

Enjoy,

Alex

Comments

# re: Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing

thanks for this post!

Monday, April 13, 2009 7:33 AM by phucnd

# re: Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing

Thank you so much for presenting this subject in a clear mannor, and it works!

Thursday, June 25, 2009 4:12 PM by Matan

# re: Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing

Thanks for this post!

Sunday, July 19, 2009 6:19 PM by kostenlos filme downloaden

# re: Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing

Dear Alex,

       I have problem with URI mapping.Please solve this issue.I have Two main pages and one navigating iframes in each main page in my project.On Visit of First main page,I redirect the Iframe source to some page(Normal SL page) on Click of Button X.I have another Button Y which on click Switches to Another main page.the issue is,Before Clicking Button Y,If i have visited any Child page(Using Iframe) in First main page,the same page is being shown in Second page Iframe.What i mean to say is, the Urimapped source(Shown in addressbar) which i visited in First main page is still shown in second main page visit.I don't want to see URI mapped source in address bar.

Can You help me regarding this.

Friday, December 11, 2009 11:42 AM by Sundeep

# re: Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing

Sundeep: I'm not sure, that I understood what you trying to do. Could you give me the complete scenario of what you trying to achive?

Alex

Friday, December 11, 2009 8:26 PM by Alex Golesh

# re: Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing

Hmmm. This Silverlight seems to be more difficult to figure out than Linux (I wonder if it's because MS wants me to buy Blend?)

I wonder if my question is similar to Sundeeps. What I want to do is basically this...

I have a logon screen. I don't want any of the hyperlinkbuttons (main page) to appear until a user has logged in. I also don't want to use a hyperlinkbutton to logon, but a regular button.

I'm looking at the default pageNavigation example in Visual Web Dev 2k8 Express. I'm wondering what the block of urimappers are at the top.

I can't for the life of me figure out how I can get a (logon) page to direct to a page (in the same project) that uses a set of hyperlink buttons to navigate within the main app.

I can be emailed at armills {at} hotmail.com

Thanks!

Tuesday, December 29, 2009 3:41 AM by Andy

# re: Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing

It was extremely interesting for me to read the blog. Thanks for it. I like such themes and everything that is connected to this matter. I would like to read more on that blog soon.

Saturday, January 02, 2010 9:31 PM by Texas Westside escort agency

# re: Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing

Can you please give me sample application for my following requirement -

I want a layout with -

1. Header.

2. Left Navigation (Collapsible Menu)

3. Content Page. (It should change as per the click on Collapsible Menu item).

4. Footer (with Current Content Page Name).

5. One Full Screen Button.

Can you please send me the above functionality ASAP. I have an urgent requirement. All should be in Silverlight Application only. Please Please .....

|---------------------------------------------------------------|

| Header.xaml                                 |

|---------------------------------------------------------------|

| | |

|left.xaml   |            content.xaml                       |

| | |

|Full Screen Btn| |

|---------------------------------------------------------------|

             footer.xaml |

|---------------------------------------------------------------|

My Mail ID: ashok.just4u@gmail.com

Friday, January 22, 2010 11:15 AM by Ashok N

# re: Silverlight 3 Quick Tip #6: Navigation Framework and Uri Routing

Ashok:

I don't have application with the spec muches you one, and will don't have a time to make your work. If you have some specific problem creating one will be glad to ANSWER your questions.

Regards,

Alex

Friday, January 22, 2010 11:31 AM by Alex Golesh

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: