Blue Theme Orange Theme Green Theme Red Theme
 
Dundas Dashboard
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Silverlight » Silverlight Tutorial: How to create an animated navigation bar

Silverlight Tutorial: How to create an animated navigation bar

This article provides step by step instructions for creating an animated navigation bar in Silverlight.

Technologies: .NET 2.0, Database, Silverlight,Visual C# .NET
Total downloads : 146
Total page views :  9259
Rating :
 0/5
This article has been rated :  0 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
TutorialNavbar.zip
 
Become a Sponsor



Follow this link to see a running example of  the end animated navigation bar result of the project

Introduction

This article is a tutorial on how to use Silverlight 2.0 to make a nifty animated navigation bar much like the one at the top of the Silverlight.net website (and also at the top of my own website SilverlightWebApps.com). When you mouse over the title you want to navigate to the background lights up with a glow and the triangular pointer zooms to the item your pointed at. When you click on the navigation item, a content panel on your silverlight page is replaced with the new selected content.

Step By Step Tutorial

A brief description of

  1. Create an empty web site in VS2008 and name it TutorialNavbar
  2. Add a new item, select Silverlight Application and save the project as TutorialNavbarXap.csproj
  3. Select the option Create a new Silverlight Project and add it to the solution:

image002.jpg

  1. Delete the TutorialNavbarTestPage.aspx file that was auto generated by VS2008. Leave the TutorialNavbarTestPage.html page. The reason I like to build my projects from a Visual Studio website as opposed to an Expression Blend project is the following:
    • It appears to me that Intellisense does not work when you open up C# files in Expression Blend (At least in the Expression Blend 2.5 June Preview version - hopefully that will be solved in production release)
    • I don't believe you can debug projects that are purely Expression Blend projects whereas if you start with a VS2008 project as I've described above, break points work normally.
    • Expression Blend creates its own default test page that you have no control over. That can be a problem, for example if you have a page that is bigger than the browser window the default Expression Blend page does not have a scroll bars. By creating your projects as I've described above, you can modify the style sheet as necessary to add these elements.

Theoretically VS2008 and Expression Blend will coordinate and keep in sync with each other so that you can seamlessly make edits to your files from within the VS2008 and/or the Expression Blend environments and have no troubles. In practice however, I have seen the two environments get out of sync and you can see a project work when you hit F5 from within Expression Blend by not from within VS2008. To get back in sync make sure all files are saved in Expression Blend, hit F5 to build it, and then from VS2008 try Build Clean Solution, then Build Rebuild All.

  1. In Expression Blend 2.5 June Preview open the newly created project TutorialNavbarXap
  2. Create something that visually looks similar to what you see below. Note to get the background effect of the green highlighting what I did was select a radial gradient brush and set the transparency of the right stop to 30%. Name the triangle pointer NavIndicator, and name each of the green highlights Nav1Highlight, Nav2Highlight, Nav3Highlight.

image003.jpg

  1. Add a canvas to the main page that takes up most of the page and name it ContentCanvas
  2. Create a rectangular that covers up the entire green highlight area Nav1Highlight and the region below where the arrow is located. Name this region Nav1ClickArea. Copy this rectangle for Nav1 and Nav2 and name appropriately. Set Opacity of each of these regions to 0%. As the name suggests this is the area that you're going to click in to allow the navigation to occur.
  3. Create some place holder pages that we can navigate to. In Expression Blend, go to the project view, click on the project and select add new item. Call the item Nav1Page.xaml and leave the "Include code file" check box checked. Repeat for Nav2Page.xaml and Nav3Page.xaml. On Nav1Page.xaml add a text block with the words "Nav1Page place holder". Repeat for Nav2Page and Nav3Page changing the stated text as appropriate.

image004.jpg

  1. Select Nav1ClickArea, go to properties, select the events button (the one with the lightning bolt) and under the event MouseLeftButtonDown enter the method name OnNav1Click.

image005.jpg

  1. Once you click enter, VS2008 should fire up and you should find that Expression Blend has entered the empty method below in page.xaml.cs. Repeat above step for the Nav2ClickArea and the Nav3ClickArea.
private void OnNav1Click(object sender, MouseButtonEventArgs e) 
{ 

} 
    • We're constructing instances of each of the pages we are going to want to display
    • We're attaching the PageLoaded method to the Loaded event and using that method to set Nav1Page as the default
    • We've added code to OnNavClick methods to remove whatever page was previously displayed and add a new page.
public partial class Page : UserControl
{ 

Nav1Page m_nav1Page = new Nav1Page(); 
Nav2Page m_nav2Page = new Nav2Page(); 
Nav3Page m_nav3Page = new Nav3Page(); 

public Page() 
{ 
InitializeComponent(); 
Loaded += new RoutedEventHandler(PageLoaded); 
} 

void PageLoaded(object sender, RoutedEventArgs e) 
{ 
RemoveAll(); 
ContentCanvas.Children.Add(m_nav1Page); 
} 

void RemoveAll() 
{ 
ContentCanvas.Children.Remove(m_nav1Page); 
ContentCanvas.Children.Remove(m_nav2Page); 
ContentCanvas.Children.Remove(m_nav3Page); 
} 

private void OnNav1Click(object sender, MouseButtonEventArgs e) 
{ 
RemoveAll(); 
ContentCanvas.Children.Add(m_nav1Page); 
} 

private void OnNav2Click(object sender, MouseButtonEventArgs e) 
{ 
RemoveAll(); 
ContentCanvas.Children.Add(m_nav2Page); 
} 

private void OnNav3Click(object sender, MouseButtonEventArgs e) 
{ 
RemoveAll(); 
ContentCanvas.Children.Add(m_nav3Page); 
} 

}

  1. Build and debug. What you should see is that when you click each of the regions the specified page should appear.
  2. Now lets make the highlights appear. As shown above with the MouseLeftButtonDown event, add OnNav1Enter method name to the MouseEnter event. Repeat for Nav2 and Nav3.
  3. Create a HighlightNone method to make all the highlights invisible and then call that from the PageLoaded method. Then add to the OnNavEnter methods a call to HighlightNone followed by a call to make the selected item highlighted. The code snippet that follows illustrates this:
void PageLoaded(object sender, RoutedEventArgs e) 
{ 
// Remove all content add then add the Nav1Page 
RemoveAll(); 
ContentCanvas.Children.Add(m_nav1Page); 

// Highlight Nav1 
HighlightNone(); 
Nav1Highlight.Opacity = 100.0; 
} 

private void HighlightNone() 
{ 
Nav1Highlight.Opacity = 0.0; 
Nav2Highlight.Opacity = 0.0; 
Nav3Highlight.Opacity = 0.0; 
} 

private void OnNav1Enter(object sender, MouseEventArgs e) 
{ 
HighlightNone(); 
Nav1Highlight.Opacity = 100.0; 
} 

private void OnNav2Enter(object sender, MouseEventArgs e) 
{ 
HighlightNone(); 
Nav2Highlight.Opacity = 100.0; 
} 

private void OnNav3Enter(object sender, MouseEventArgs e) 
{ 
HighlightNone(); 
Nav3Highlight.Opacity = 100.0; 
}

  1. Build and debug. What you should see is that whenever you mouse over the different navigation sections the highlight appears.

image006.jpg

  1. Now lets animate the NavIndicator triangle. Create a storyboard called MoveToNav2.

image007.jpg

  1. Without making a key frame at time zero move the timeline to 1 seconds, then drag the NavIndicator triangle to a centered position under Nav2.

image008.jpg

  1. Click on the key frame marker for the NavIndicator and adjust the easing to x1=0 and x2=.5. If you don't see the easing box below when you click on the NavIndicator key frame, make sure the properties box is set to properties and not events. Hit play on the storyboard to see the effect of these adjustments. Close the storyboard once you're done.

image009.jpg

  1. Now go to the XAML view and see what was generated for this storyboard. You'll see two sections that begin DoubleAnimationUsingKeyFrames. The first one animates the X direction as indicated by the line Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X). The second DoubleAnimationUsingKeyFrames animates the y direction as indicated by the TranslateTransform.Y item. We only want the NavIndicator to move horizontally, so delete the second DoubleAnimationUsingKeyFrames Section.

image010.jpg

  1. Now make a copy of the entire storyboard but change two things:
    • Change the name of the storyboard to MoveToNav1 as shown here: <Storyboard x:Name="MoveToNav1">
    • Change the SplineDoubleKeyFrame Value to 0 as shown here: <SplineDoubleKeyFrame KeyTime="00:00:01" Value="0">

What this does is makes the same storyboard but instead of moving to the Nav2 position it move to back to where it started (i.e. 0). We're going to do the same for MoveToNav3. How do the value to move to for Nav3? I actually created a MoveToNav3 storyboard using the Objects and Timelines menu, read the value of the X move and then copied that value into my own XAML storyboard that I based on the MoveToNav2 storyboard above. If you didn't want to work with the XAML, you could repeat the steps listed above for the original MoveToNav2 storyboard for each of the other moves.

  1. Now add code to start the corresponding animation whenever the mouse passes over the navigation area.
private void OnNav1Enter(object sender, MouseEventArgs e) 
{ 
HighlightNone(); 
Nav1Highlight.Opacity = 100.0; 
MoveToNav1.Begin(); 
} 

private void OnNav2Enter(object sender, MouseEventArgs e) 
{ 
HighlightNone(); 
Nav2Highlight.Opacity = 100.0; 
MoveToNav2.Begin(); 
} 

private void OnNav3Enter(object sender, MouseEventArgs e) 
{ 
HighlightNone(); 
Nav3Highlight.Opacity = 100.0; 
MoveToNav3.Begin(); 
} 

  1. Hmm… One second is a little slow for that transition. Let's change the time for the transition to .5 seconds. Go to the XAML and change the time from 1 second to a half second
<Storyboard x:Name="MoveToNav1">

    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"… 

        <SplineDoubleKeyFrame KeyTime="00:00:00.50" Value="0">

  1. Much better! That's it. You now have an animated navigation bar.

History

July 2008, First Revision


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Mike Dobbles
Mike Dobbles is a senior software developer with 15 years experience.  He is currently the principal at SilverlightWebApps.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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
TutorialNavbar.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved