![]() |
Web Development »
Silverlight »
General
Beginner
License: The Code Project Open License (CPOL)
Silverlight Tutorial: Creating an animated navigation bar using StoryboardsBy Mike DobblesFirst article in a series. This article focuses on the use of storyboards for creating animation effects. |
C#, .NET, Silverlight, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Follow this link to see a running example of the end result of this project
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.
This article is the first in a series. While in this article I focus on how to manually connect up storyboards to mouse event in the second article I show you how to do this using the visual state manager.
A brief description of

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.



private void OnNav1Click(object sender, MouseButtonEventArgs e) { }
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); } }
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; }





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.
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(); }
<Storyboard x:Name="MoveToNav1"> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"… <SplineDoubleKeyFrame KeyTime="00:00:00.50" Value="0">
July 2008, First Revision
August 2008, updated to reference 2nd article in series.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 4 Aug 2008 Editor: |
Copyright 2008 by Mike Dobbles Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |