nRoute: More Wholesomeness, with SL 4 and .NET 4.0

Posted by Rishi on 28-Apr-10 1:10 PM - Comments (10)

Tags: , , | Categories: .NET, nRoute, Silverlight, WPF

Following RTM of Visual Studio 2010, Silverlight 4.0 and .NET Framework 4.0, we've update nRoute to target Silverlight 4 for the Web and .NET 4.0 for the desktop. Though the feature-set of the new release is essentially the same as the previous release, this is more than just a re-compiled version - we've tweaked the code to take advantage of the binding enhancements and the introduction of custom/predefined xml-namespaces in Silverlight 4.

Binding Enhancements

As you already know Silverlight 4 (SL4) significantly improves the binding story, gone are the limitations of only being able to bind to FrameworkElement derivates - we can now, in parity to WPF, bind to any Dependency Object. Taking advantage of this we've removed nRoute's custom binding infrastructure (though it remains for WP7) and lowered the barrier to entry for a lot of behaviors and triggers as we aren't anymore limited to inline FrameworkElements only.

One breaking but useful change we've made is to make Url parameters bindable - as you know when you effect navigation or controller-actions in nRoute you can optionally pass these key-value pairs of parameters, kind of like with form collection in HTML. However, earlier owing to the binding limitations of Silverlight you were limited to using statically declared parameters (or bind to the parameter's collection in its entirety) - but now with the binding enhancements we can simply bind against each parameter directly, see:

   1: <Button Content="Customer Overview" Height="25" Width="100">
   2:     <i:Interaction.Triggers>
   3:         <i:EventTrigger EventName="Click">
   4:             <n:NavigateAction Url="Pages/Customer/Overview">
   5:                 <n:DependencyParameter Key="Customer" Value="{Binding ActiveCustomer}"/>
   6:             </n:NavigateAction>
   7:         </i:EventTrigger>
   8:     </i:Interaction.Triggers>
   9: </Button>

Above, we've attached an event-trigger (line 3) which executes a NavigationAction (line 4), and with it we pass a keyed parameter (line 5) which gets its value through binding - this wasn't possible earlier, though a small one it makes life that much easier. And by the way, if the xaml above looks scary, not to worry, the entire thing is perfectly Blendable.

DependencyParameters

For the sake of completion, I'll point out that you can receive the passed in parameters (like above) by implementing ISupportNavigationLifecycle interface in either the View or the ViewModel. As an example, below we're using the built-in NavigationViewModelBase class (which already implements ISupportNavigationLifetime), and we override its OnIntialize method to get hold of the passed-in parameters.

   1: [MapViewModel(typeof(CustomerOverview))]
   2: public class CustomerOverviewViewModel : NavigationViewModelBase
   3: {
   4:     protected override void OnIntialize(ParametersCollection state)
   5:     {
   6:         this.Customer = (CustomerInfo)state["Customer"];
   7:         base.NotifyPropertyChanged(() => Customer);
   8:     }
   9:     public CustomerInfo Customer { get; private set; }
  10: }

Except for the fact that it's not strongly-typed, this is a pretty convenient way of passing-in values - and in fact it also answers a common MVVM question about how do we pass-in values to the ViewModel. As I mentioned earlier passing parameters also works with controller-actions, so below is a simple example showing exactly that:

   1: <Button Content="Analyse Customer">
   2:     <i:Interaction.Triggers>
   3:         <i:EventTrigger EventName="Click">
   4:             <n:ExecuteControllerAction Url="Tasks/Customers/Analyse">
   5:                 <n:DependencyParameter Key="Customer" Value="{Binding ActiveCustomer}"/>
   6:             </n:ExecuteControllerAction>
   7:         </i:EventTrigger>
   8:     </i:Interaction.Triggers>
   9: </Button>

Note the similarity between ExecuteControllerAction and NavigateAction, which is by design. And, on the receiving end, the Controller looks something like this:

   1: [MapController("Tasks/Customers/{Action}")]
   2: public class CustomerController : Controller
   3: {
   4:     public void Analyse(CustomerInfo customer) 
   5:     {
   6:         // business logic to analyse the customer
   7:     }
   8: }

Above note how the "Customer" parameter is consumed in a strongly-typed manner, and the "{Action}" token is transposed to the method name (like in ASP.NET MVC). And I'll also just pin-point that the xaml above is entirely generated by Blend.

Default Xml-Namespace

Having to define separate xml-namespaces for each and every control, behavior, element etc has been a pain-point in Silverlight for only forever - but now with SL4's support for statically declare xml-namespace definitions, we've now got a simplified way to represent most of nRoute's functionality using just a single xml-namespace declaration.

XmlNamespace 
As you can see above, the newly minted xml-namespace for nRoute is "http://nRoute/schemas/2010/xaml", along with its preferred prefix of "n". This works with both the full nRoute Framework and the Toolkit version, and against both Silverlight 4 and WPF but not with WP7. Also, for reasons unknown to me, tooling support in both Blend and Visual Studio seems to work better when using the predefined xml namespace - like above, we've got full intelligence support which was patchy before (note above we're using the awesome "Xaml Intellisense Presenter" plugin by Karl Shifflett).

Other Changes

Because now WPF supports the Visual State Manager (VSM), we've unified WPF and Silverlight's controls templates for all the Navigation Controls. Also, the default ErrorPage is now shared between the two frameworks. And since Silverlight now provides an Unloaded event, we've updated the BridgeViewModel behavior to allow calling a specified command when the View is being unloaded. We've also update support for Blend related tooling, specifically with behaviors and triggers in nRoute.

Updated Targets and Dependencies

The new release is versioned 0.4.5, while the one last week was 0.4.0; and summarized below for both the releases are their dependencies and target requirements:

Target/Version nRoute Version 0.4.0 nRoute Version 0.4.5 (NEW)
Web Silverlight 3 Silverlight 4
Desktop .NET Framework 3.5 sp1 .NET Framework 4.0
Mobile WP7 (Silverlight 3) N/A
     
Dependencies - Blend 3 SDK 
   for System.Windows.Interactivity.dll
- Reactive Framework
   for System.Observable.dll
- Blend 4 SDK, available with Blend 4 RC
   for System.Windows.Interactivity.dll
- Reactive Framework (only for Silverlight 4)
   for System.Observable.dll

The one notable above is the lack of support for WP7 in this release; it's because the WP7 tools haven't been updated for use with VS 2010 RTM - once they do, we'll update against it. However, you can use the earlier release, as there is no newly added functionality that targets Silverlight 3. Also as a cautionary note, if you are updating an existing nRoute application, be sure to update both System.Windows.Interactivity [version 4.0.x..] and Microsoft.Expression.Interactions [version 4.0.x..] else you'll get some rather cryptic and unhelpful errors.

Download the dlls this release from Codeplex
Read about what else is included this release, here.

Comments

pingback
carlosga.wordpress.com
on 28-Apr-10 8:30 PM
Pingback from carlosga.wordpress.com

nRoute v0.4.5 para Silverlight 4 y .NET 4 « Carlos Blog

Marcos Tabaj
Marcos Tabaj France
on 30-Apr-10 3:26 AM
Hi Rishi,

Your work it's excellent, it is very difficult to find the actual way of working.  All the samples are outdated.

I think your work deserves a good, simple but complete exemple of working with the SL4 framework (even if the UI is not outstanding as usual).

Thanks a lot for share your work.

Marcos

Rishi
Rishi
on 30-Apr-10 12:48 PM
@Marcos, appreciate your input. I understand the frustration with samples and documentation, and I'm working on both. Now, as a first step, I'll convert most of the old samples to the latest version and post them on Codeplex. Secondly, I'm trying to consolidate the documentation, and put it up as a single pdf. Third, I wanna put up some screencasts showing some of the basics and finer points of nRoute.

Post these steps, I'll look at getting a proper end-to-end sample up. Now, though I and others have used nRoute in end-to-end applications, I can't share those as they are proprietary to various customers - which is kinda sad, as some of them are really good at showcasing nRoute's potential. All the same, if you have any ideas for an end-to-end application, I'll be happy to entertain your suggestions.

PS: Check back next week for some updated samples.

Nic
Nic Canada
on 17-May-10 2:08 AM
Hi Rishi,

Any eta on the updates samples?

Thanks!

Rishi
Rishi
on 17-May-10 1:29 PM
@Nic I've converted most of the old samples to SL4, n' I'll put them up tomorrow. However I've not been able to get time to finish up a LOB sample app that highlights some of the newer features.

Nic
Nic Canada
on 18-May-10 2:28 AM
Hi Rishi,

That would be perfect. It will give those of us who want to get familiar with the basics a head start.

Thanks again.


trackback
orktane
on 19-May-10 2:58 PM
nRoute Samples Revisited

nRoute Samples Revisited

pingback
116.animejin.com
on 22-May-10 5:56 AM
Pingback from 116.animejin.com

Moto Clube Rd350, Sc430 Forum Club Lexus

AK Srivastava
AK Srivastava India
on 10-Jun-10 2:00 AM
Hi Rishi,
Please help me in understanding navigation in SL4.
like:
1.Listbox contains IEnumerable<FileCategory>.
2.Selected item in listbox is SelectedFileCategory.
3.Now, after selecting a FileCategory from listbox, if I click Edit HyperlinkButton, i should go to a page called EditFileCategory.xaml (using view model EditFileCategoryViewModel), with the SelectedFileCategory parameter passed to the EditFileCategoryViewModel...

?? How to do that??

Also, if you can give a complete documentation of navigation, it will be of great help to me..

regards
Ashutosh

Rishi
Rishi
on 13-Jun-10 11:20 PM
Hi Ashutosh, given your requirements you could use something like:

<Grid x:Name="LayoutRoot" Background="White">
  <HyperlinkButton Content="Open File Category" Height="20" Margin="8,8,8,0" VerticalAlignment="Top">
    <i:Interaction.Triggers>
      <i:EventTrigger EventName="Click">
        <n:NavigateAction Url="Pages/EditFileCategory/">
          <nLaughingependencyParameter Key="FileCatagory" Value="{Binding SelectedFileCategory}"/>
        </n:NavigateAction>
      </i:EventTrigger>
    </i:Interaction.Triggers>
  </HyperlinkButton>
  <ListBox Margin="8,28,8,20" ItemsSource="{Binding FileCategories}"
    SelectedItem="{Binding SelectedFileCategory, Mode=TwoWay, UpdateSourceTrigger=Explicit}">
    <i:Interaction.Triggers>
      <i:EventTrigger EventName="SelectionChanged">
        <n:UpdateBindingExplicitlyAction PropertyName="SelectedItem"/>
      </i:EventTrigger>
    </i:Interaction.Triggers>
  </ListBox>
</Grid>

Now, for a better guide on how all the pieces could be brought together see the Officer Xcel Demo. It especially makes use of navigation based on listbox selection changes and shows how you can use navigation containers.

Also you can read about the basics of navigation and containers in nRoute from (though things have changed a bit) www.orktane.com/.../...equest-In-Response-Out.aspx and www.orktane.com/.../...ers-It-holds-the-Magic.aspx

I'll try and put out more documentation.
Hope this helps,

Rishi

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading