WP7 NavigationService

Aug 31, 2010 at 9:41 AM

In the documentation for the WP7 bits, there is a nice example of the NavigationService:

navigationService.Navigate(new Uri("/PageTwo.xaml?NumberOfTabs=5", UriKind.RelativeOrAbsolute));

I was wondering what the preferred approach is for passing objects to the viewmodel. Suppose for example I want to do something like this:

public void ViewDetails(Book book)
 navigationService.Navigate(new Uri("/PageTwo.xaml?Book=" + book, UriKind.RelativeOrAbsolute));

This is obviously not supported, but I was wondering what the preferred way of doing this was. I can of course easily pass the id and then query again in the targeted view model, but I'd like to avoid the second query


Aug 31, 2010 at 12:28 PM

What you have discovered, I believe, is one of the flaws in the Silverlight navigation framework's approach to navigation. Unfortunately, if you are building for WP7, you have to use it, at least for your first page. As a result, there's no *real* way to pass data besides query strings. What you could do is create a cache that allows simple lookup by id and have your destination just pull the data out of that cache. The cache could be async and have some smarts so that if the data isn't found, it then goes and performs the real query. In that way you can avoid the double query when you are doing a page transition from in app, but still have a way to get the data if your app is resurrected without the instance data in memory. Another approach would be to abandon the View-First mechanism imposed by WP7. In that case you would use the first page as a Host for the real navigation mechanism, controlled by a Conductor<T>. You could then use a ScreenSubject approach to pass data to individual screens. You can have a look at how this implemented in the full version of Caliburn. Look for things related to the IHaveSubject interface. However, if you take this approach, you still have make sure that during tombstoning you record the state of the Conductor (you could use the [SurviveTombstone] attribute) and also record the data or it's ID for use if resurrection occurs.