NavFx Part 1 - Quick Reference

In this first article on NavFx I am going a provide a quick reference for it's features by way of some basic documentation. Since posting the first version of this article I have started a project on CodePlex for NavFx and made the code available there, along with simple demo projects in C# and VB.NET.  I have also created VSI installers for templates that you can use to get up and running with NavFx very quickly.  The project is located at http://www.codeplex.com/NavFx.

Package Contents

NavFx includes four interfaces, five classes and seven custom exceptions, which are listed below and will be explained further in this article.

  • IApplication
  • IHostPage
  • IPage
  • ITransitor
  • Navigator
  • Application
  • HostPage
  • Page
  • SimpleTransitor
  • AssemblyLoadException
  • InconsistentIndexException
  • PageNotRegisteredException
  • PageTypeNotSupportedException
  • TargetNotSetException
  • TargetNotSupportedException
  • TransitorNotSetException
  • NavFxApplication Template
  • NavFxHostPage Template
  • NavFxPage Template 

NavFx Navigator

At the heart of NavFx is a class called Navigator. Navigator provides all of the built in functionality provided by NavFx, but it relies on a page tranisition handler that you provide.  The diagram shows the methods provided by Navigator:

 

Navigator class diagram
Method Description

CurrentIndex

 Index of the page currently being displayed.  This can become inconsistent when the basic GoToPage method is used
CurrentPage  Path value of the page currently being displayed
IsIndexConsistent Indicates if the CurrentIdex is consistent with the page currently being displayed.  This can become inconsistent if the basic GoToPage method is used.  If this reports false then calling NextPage or PreviousPage can give unexpected results
Transitor Reference to the ITransitor you have set Navigator to use for handling page transitions
ClearCache Removes pages from the internal cache maintained by Navigator.  By default this method does not remove any Pinned pages, set the clearPinned argument to true to have Navigator remove all pages.  Pages are also removed from the Forward and Backward history caches
GetPage Retrieves a page from the cache.  The basic version of this method returns the IPage interface of the page.  Use the generic version to retrieve the page as a specific type
GoBack Displays the last page in the backward history stack if available. It returns true if are more pages in the forward history stack, otherwise false.
GoForward Displays the last page in the forward history stack if available. It returns true if there are more pages in the forward history stack, false otherwise.
GoToPage Displays a page specified by its path. There is an overload that takes a Uri and if the page is not loaded in the page cache will load the pages from the library.
IsPageRegistered Returns true if a page with the specified path is already in the page cache, false otherwise.
IsPinned Returns true if the page with the specified path is Pinned in the cache, false otherwise
LoadPages Accepts a Uri and loads all pages that implement the NavFx IPage interface into the page cache, optionally displaying a page specified by its path.
NextPage Displays the next page in the page cache based on load order.  Intended to support Wizards it returns true if there are following pages in the page cache.
PreviousPage Displays the previous page in the page cache based on load order.  Intended to support Wizards it returns true if there are preceding pages in the page cache.
RegisterPage Adds a page implementing the NavFx IPage interface to the page cache
RemovePage Removes the page witht he specified path from all caches.

 It is important to understand the differences between two pairs of methods.  GoForward, GoBack, NextPage and PreviousPage.  The difference between the pairs is what they navigate against.  Navigator maintains three internal caches of registred IPage objects, Pages, ForwardHistory and BackHistory.

Pages is a cache of all pages that have been registered with Navigator and is a navigable collection.  ForwardHistory is a stack on to which the current page is pushed when you use the GoBack method.  Back History is also a stack and the current page is pushed on to it whenever you navigate away from it.  GoForward will pop the page at the top of the ForwardHistory stack and GoBack will pop the page at the top of the BackHistory stack.

NextPage and PreviousPage on the other hand do not use the forward and backward history stacks.  Instead they allow you to navigate  through the Pages collection in page load order.

All four of these methods return true if there is another page available in the relevant direction, this is intended to make it easy to implement navigation buttons.  If you call one of these methods from a button and the method returns false, disable the button, better still assign the return value directly to the IsEnabled property of the button.

NavFx Transitions

Although Navigator provides all of the methods you need to navigate around your Silverlight 2.0 application it does not handle the transition from the current page to another.  Navigator relies on transition handlers or Transitors to do the work of changing between pages.  Transition handlers must implement the ITransitor interface, which defines a single method TransitionPage that accepts two intances of IPage previousPage and nextPage.  How the transition from one page takes place is entirely up to the Transitor, for example you might want to animate the transition from the current page to the next.  Navigator will call the TransitionPage method of the ITransitor that you provide it with.  Navigator takes care of maintining backward and forward history, but you have to take care of how the transition happens.

NavFx includes a concrete class called SimpleTransitor, which ignores the previousPage and replaces the content of a container you specify with whatever you pass as the nextPage.  The Target property of SimpleTransitor can be an Application, UserControl or Panel.  The demo project uses the SimpleTransitor exclusively so there are plenty of examples of using it available.

NavFx Interfaces

ITransitor was explained in the previous section, the following diagram presents the three additional interfaces defined by NavFx.  NavFx provides concrete implementations of these interfaces that will suffice for most Silverlight applications that you create, but if you prefer to stick to defining your pages as standard UserControls or need to add NavFx to an existing application with minimal changes these interfaces are the way to go.

NavFx intefaces class diagram

IPage

Implementing IPage is required to enable your Silverlight pages to be registered with Navigator.  It ensures a path is available as a key for accessing the page in the internal caches of Navigator.  The IsPinned property supports pinning selected pages in the cache so that ClearCache will ignore them when cleaning up.

IHostPage

Implements IPage and is intentended for implemenation by pages that host other pages.  The main thing it ensures is the convenience of a local reference of a Navigator to use for navigation between the hosted pages.  The demo project contains several examples of this scnerio, for example the Wizard example is a host that enables you to navigage between its three pages.

IApplication

Implementing IAppication is optional, it defnes simply that an application must provide a method to get a Navigator.  This is an important interface for pages implemented in external libraries, it enables them to get a reference to the Navigator from the current application something like this:

Navigator nav = ((IApplication)Application.Current).GetNavigator();

or

Dim nav as Navigator = CType(Application.Current, IApplication)

Concrete NavFx

"So interfaces and all that are really good but I just want something I can use" I hear you saying.  Well NavFx has you covered as it includes concrete classes that implement the interfaces introduced in the previous section.  The following class diagram presents the concrete classes that you can use instead of the built in Application and UserControl provided by Silverlight.

 

The usage of these classes should be fairly obvious from the explanations of the interfaces because they are direct implmentations.  For completeness and to hightlight some noteworthy points the classes are covered in the following sections.   To create your Silverlight application using these classes simply install the templates with the VSI avaiable from the CodePlex releases page of the project.

Application

This is a subclass of the System.Windows.Application that instantiates a Navigator and makes it available via the GetNavigator method.  You still need to prepare and wire up a tranistion handler (ITransitor), I did not want to couple things too tightly so left that piece of work to you.  The demo project includes an example of how to do this and you can simply copy and paste the code.  GetNavigator does not instantiate the Navigator until you first call it.

HostPage

This is a concrete implemenation of IHostPage ready for you to add content and a container for your pages.  The SetContent method simply sets the protected Content propery to whatever you pass.  SetContent is intended to allow your ITransitor implemenations to set the Content property of a page, which is Protected so can't normally be set except by subclasses.  You should override SetContent particularly in HostPages so that Navigator displays pages in the right container.

The Navigator property of HostPage checks the the current application for the IApplication interface and calls GetNavigator.  If IApplication is not implemented it throws a NotImplementedException.  Similarly the setter for the Navigator property throws a NotImplementedException.  The property is virtual so you can override it if you wish.

Page

Is a concrete implemenation of IPage and provides a default implemenation of the Path property that returns the name of the class.  In simple applications like the demo this will be all you need, the pages are all named for their purpose and using the name of the class as the key for retrieval from the page cache works fine.  HostPage inherits this behaviour.

IsPinned returns false by default but you can set it otherwise in application code or override the property to return true.

Templates

The demo project download includes three templates that will help you get started with new Silverlight 2 applications using NavFx.  NavFxApplication.zip is a Project template that will create a new application with a page structure that matches the demo project.  NavFxHostPage.zip is an Item template for adding new HostPages to a project.  NavFxPage.zip is an Item template for adding new Pages to a project.

Download an execute the VSI from the releases page at http://www.codeplex.com/NavFx and the templates will be added to the My Templates section of the New Project and New Item dialogs in the Silverlight category.

Well that is it for this article.  I have commented the demo project throughout so you should have no problems getting started.  I will continue this series with a set of walkthroughs for different scenarios and how to implement them with NavFx.

Next Article - Setting Up

 

Print | posted @ Saturday, June 21, 2008 1:03 PM

Comments on this entry:

Gravatar # re: NavFx Part 1
by Thomas Holloway at 6/27/2008 8:30 PM

Hi Mike! Great article. You can create a Visual Studio Installer by creating a .vscontent xml file and including the following:
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>NavFx.zip</FileName>
<DisplayName>Name Here</DisplayName>
<Description>Your description</Description>
<FileContentType>VSTemplate</FileContentType>
<ContentVersion>2.0</ContentVersion>
<Attributes>
<Attribute name="ProjectType" value="Visual C#"/>
<Attribute name="ProjectSubType" value=""/>
<Attribute name="TemplateType" value="Project"/>
</Attributes>
</Content>
</VSContent>

Then simple zip both the .vscontent and your included zip file together. Then rename that zip file to a .vsi and there's the Visual Studio Installer :D
  
Gravatar # re: NavFx Part 1
by Mike Hanson at 6/27/2008 10:33 PM

Thanks Thomas I will get one ready with an update to NavFx I am almost ready with.
  
Gravatar # re: NavFx Part 1
by Fallon Massey at 6/30/2008 7:32 AM

I really like the direction you've taken, but I have one complaint, and it bothers me a bit.

The structure of your library forces me to alter header information of the XAML & codebehind. I just can't utilize user controls as is.

I'm not sure how much trouble this is in migrating a design, but I do know that it makes the existing tooling difficult to use and keep in sync with your stuff.

Other than that, this is a really nice approach, and I especially like the transparent(to the developer) use of loading assemblies... really sweet.
  
Gravatar # re: NavFx Part 1
by Mike Hanson at 6/30/2008 3:16 PM

Fallon: You can achieve what you want simply by implementing the interfaces yourself.

So you can take a standard UserControl, then in the code behind only, implement the IPage interface or the IHostPage interface.

The templates provide an easy way to get started and a means for using NavFx with minimal effort and custom code. Effectively they are custom controls, but you are not forced to go that way.

I will include an article on doing it this way on my blog in the not too distant future.

Mike
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Fallon Massey at 7/1/2008 9:21 PM

Thanks Mike,

I look forward to that info.

BTW, you may want to let more people in the SL Community know about what you've done. Getting linked on Silverlight Cream would be a good thing.
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Mike Hanson at 7/2/2008 6:05 AM

Silverlight Cream is a new one on me will take a look. I did post in the Silverlight.net gallery but nothing has appeared so either they have rejected it or not gotten around to processing it yet.
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Fallon Massey at 7/2/2008 9:07 AM

The Gallery is more for somewhat finished apps, or apps with some sort of UI to demo, you aren't there yet.

The two places developers will see you are these.

1. Silverlight Cream - http://geekswithblogs.net/WynApseTechnicalMusings/Default.aspx

2. Silverlight Sideshow - http://www.silverlightshow.net/

A posting in either or both of these will get you a lot of hits. I'd like to see this get more press, good luck.
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Mike Hanson at 7/2/2008 9:45 AM

Posted to both, thanks Fallon
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Fallon Massey at 7/4/2008 8:48 AM

Good work Mike, I saw you in both places.

I was wondering if you saw this article and code for some base transitions.

http://blogs.msdn.com/devdave/archive/2008/06/13/navigation-with-transition-effects.aspx

From the design of the code, I think it might be easy to include into the library as a start for people(obviously your decision).
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Mike Hanson at 7/4/2008 10:28 AM

Absolutely I think I have NavFx stable as it is now I am using on two applications without problems so apart from adding articles the next obvious things to do are extend the demo to really show things off and add some Transitors. I will take a look at the examples and use the implemenation of one as a basis for an article on how to do it then add more to the project perhaps in a seperate library to keep NavFx lightweight
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Fallon Massey at 7/6/2008 8:38 PM

I don't know if you changed the license, or I just noticed it, but is there a reason you're using GPL, and not LGPL?

If you stick with GPL, you need to let people know this way up front, because GPL is not looked on favorable in the MS/.NET world, and I certainly can't use it.

If you can't change it, I'll understand, fortunately Holloway's information is freely available.

Fallon...
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Mike Hanson at 7/7/2008 9:44 AM

No particular reason, I just want to share this with as many people as possible. I am new to Open Source and quite frankly already bored with the political side of it. I would happily publish without a license but CodePlex doesn't allow that.

I did read a couple of articles trying to find the best choice and GPL seemed to be the most common one used, I haven't changed it since publishing the project. I will change it to LGPL if that is the most open licence that is acceptable to those who want to use it.

If it becomes an issue I will just make the code and framework available from my own site and skip the whole licensing issue.
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Fallon Massey at 7/7/2008 9:58 AM

There are real issues behind what appears to be political debates.

GPL REQUIRES that ALL applications we create be open source and that we must expose everything we do to everyone.

That's silly if a library is a very small piece of a much larger work. That's why LGPL was created, to distinguish betweed a library and a complete application.

The BSD is a good license as well, but to the polotics, Microsoft believes that if it's open source, you should be free to do what you want with it. GPL doesn't believe that, they believe in the viral nature of forcing everything to be open.

Either way, the company I work for would never allow GPL, unless it was a complete application. If it's a library, which is a small part of what we would be doing, it has to be something else.

Sorry for being so long winded.

BTW, where are you located, your time zone seems way different. I'm in Los Angeles, and you appear to be west of me... Hawaii?
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Mike Hanson at 7/7/2008 11:12 AM

I will do some more reading on Open Source licences, I have changed to LGPL now so you should be ok to use NavFx.

I am in the UK, just outside the ancient Roman city of St Albans, just north of London, England.
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Fallon Massey at 7/7/2008 6:36 PM

Thanks Mike!
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Mike Hanson at 7/7/2008 8:07 PM

Having got home I have read all of the licences supported by CodePlex from top to bottom. I really wish for once I had started at the end because the closest to what I want was the last one.

It pretty much says you can do anything that you want with the code, which is exactly what I want people to do. It did require me to enter a year and copyright owner, but I simply entered NavFx Project which in my mind is anyone who downloads the code.

Hopefully this agreeable to everyone.
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Fallon Massey at 7/8/2008 12:48 AM

Good to hear Mike, I thought that was the spirit you wanted, but I much prefer to let you express your original intent.

I can't imagine anyone not agreeing with tohse terms.
  
Gravatar # re: NavFx Part 1 - Quick Reference
by David Roh at 7/29/2008 12:05 PM

Hi Mike,

I really appreciate your taking the time and effort to share this - also thank you for licence choice.

David Roh
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Mike Hanson at 7/29/2008 12:23 PM

Thanks for the feedback David, I am pleased so many people are benefitting from it.
  
Gravatar # re: NavFx Part 1 - Quick Reference
by Coupon Codes at 5/15/2009 4:57 PM

This is just the information I was looking for. Great resource, site bookmarked.
  
Gravatar # re: NavFx Part 1 - Quick Reference
by tutti i siti di casinò at 10/14/2009 12:42 PM

I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.Any way Ill be subscribing to your feed and I hope you post again soon...
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 8 and 1 and type the answer here: