In this article I will present the different ways to use nRoute for the three platforms it is built for. So first create a Silverlight, WPF or WP7 project.
We can now add a reference to nRoute and to System.Windows.Interactivity (used for behaviors, triggers, etc…).
The screenshot above shows how the reference virtual folder should look like after the references have been added to you project.
Setting up nRoute
In order to be able to use nRoute we have to initialize it before. Let’s modify the App.xaml file first.
Setting up nRoute is different for each Silverlight, WPF and WP7. Here’s how to do for each platform.
The namespace used for nRoute is :
It will be used for all examples except those for WP7 because it is not available for this platform.
Add nRoute’s application service to the ServiceLifetimeObjects property of our application.
Windows Phone 7
The same as Silverlight here except for the namespace.
The ApplicationLifetimeObjects property doesn’t exists in WPF therefore we have to do something special here. We have to make our Application inherits from nRoute.ApplicationServices.Application
Then the code-behind
Creating the view
The view we will use here is over simple and will be the same on the three platforms.
Creating the ViewModel
nRoute provides a base class for ViewModels that implements the INotifyPropertyChanged interface and provides the NotifyPropertyChanged method taking a lambda parameter instead of a string.
I really appreciate this approach as it helps avoiding a lot of errors when refactoring the classes.
The secondary effect of this is that has a heavy performance cost. I invite you to read this post if you are interested in the performance issues with the different INotifyPropertyChanged implementations :
Using this base class is not required for nRoute. You can choose what implementations fits the best your needs or your tastes.
Here is our ViewModel (note the lamba call within NotifyPropertyChanged).
Associating the ViewModel and the View
Now that we have a view and a view-model we need to associate them. This is done with attributes (like many things in nRoute).
There is two ways to do that :
Association from the ViewModel with MapViewModel attribute.
Association from the view’s code-behind with MapView attribute.
You can choose either way. I generally use MapViewModel for most of the cases.
These attributes allows nRoute to build a relationship between your views and view-models but they do not lead to the automatic injection of the view-model in the DataContext property of the View. This injection is done with a behavior.
We will now add the nRoute and System.Windows.Interactivity namespaces to our view and add the behavior.
When nRoute is initialized, it will override the default behavior of Silverlight, WPF or WP7 mainly thanks to the Resource Locator Framework (aka RLF). When BridgeViewModelBehavior is attached to an object, it uses the RLF in order to find a view-model corresponding the current object thanks to the MapViewModel and/or MapView attributes.
So here is the glue. This is this behavior combined with the RLF that allows the externalization of the view-model’s creation.
You now how to intialize nRoute for Silverlight, WPF and WP7 and how to associate a view-model to your view with the BridgeViewModelBehavior.
You have seen the ViewModelBase class with its benefits and drawbacks.
In the next article I’ll show you how to create commands and revers commands with nRoute.