How to combine Silverlight Navigation with the Caliburn.Micro framework, with a bit of MEF thrown in!
I created this navigation sample after trying to combine the Caliburn.Micro MVVM framework with a Silverlight navigation application that I was currently working with. Out of the box, CM does not support this. The whole purpose of the navigation is so that
the foward and back buttons of the browser work to navigate correctly within your SL app. If your SL app doesn't support this, and your user tries to use the back button, he will be thrown out of your app to the prior page he was on such as his home page.
Not a nice experiance and probably not what he expected. Further, you want to be able to have a link that goes all the way down to a specific product or item. The end user should be able to email that link to someone and have them click on it and go directly
there. Again, having the navigation enabled is the only way to accomplish this.
The issue is that a SL nav application is required to work with a Frame inside of which all navigation is done. The navigation is based on a URI to a specific location. This is pretty much as far as you can get from an MVVM approach and there is no way around
this. Or so I thought. After quite a bit of digging, I found that you there is an interface call INavigationContentLoader and by using this, you can catch the call of the navigation and do what you will with it.
In this sample application, I used MEF to find the page to navigate to and instantiate it, then use MEF again to find the ViewModel for the page, and then use Caliburn.Micro to bind the two together. This gives us the best of both worlds, the Navigation benefits,
and the benefits of MVVM.
To implement this in your own project, you don't really need to know either MEF or Caliburn.Micro too deeply. There are a couple of nuggets in implementing them, but you will find those easy to master and are covered in detail in the documentation. But
you can easily get up and running just starting with this sample application. All the code to make it work has already been written. This was the hard part. All you need to do is just to use is an attribute on your Page and ViewModel so MEF can find them and
marry them up. If you have any questions, feel free to post over on the discussion page.
See my Silverlight Blog at SilverlightDev.net
- For the latest Caliburn.Micro, see
http://caliburnmicro.codeplex.com While this project includes a copy of Caliburn.Micro.Silverlight current at the time of project creation, you should obtain a copy of the latest version before implementing it in your own project. Caliburn.Micro is updated
frequently by it's author, Rob Eisenberg.