30 March, 2011

SimpleMvvmToolKit – now with navigationsupport

Having used the SimpleMvvmToolKit for a while, I’ve come to see that this also is missing out on the inherent problem about navigation in a WP7 application when you are using MVVM as design foundation. According to Tony Sneed, the way navigation should be taken care of is by using the MessageBus and/or Notifications. I find this to be a little too loosely coupled for my taste. I feel that a WP7 framework should at least support navigation from the ground up and not be some kind of an afterthought?

Previously, I’ve been discussing a dedicated NavigationController (at that time referred to as ApplicationController) in a WP7 application. This concept is quite a good invention, as it allows you to specify the whereabouts of your Views in one place only. And next navigate in the application in a type safe way using enums as references. Last but not least, you leave the navigation functionality to dedicated controller that handles this very task: to navigate. I severely prefer the Separation of Concern principle. Due to this reason, I’ve added support for NavigationController in an improved version of the SimpleMvvmToolKit.

image

How do you use this NavigationController baked into the SimpleMvvmToolKit?
First – you need to create a simple enum that states the various Views that exists in your application.

public enum ViewType
{
Home,
Security,
Settings,
Trade,
PropertyEditor,
About,
Theory,
SecurityHistoryView,
ErrorView
}

In you app.xaml (where life starts); you register this enum alongside their location in your application.

// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
void Application_Launching(object sender, LaunchingEventArgs e)
{
//init data
PortfolioManager.InitializeDataStorage();
IsolatedStorageHelper.ClearRuntime();

SetupNavigationController();
}

private void SetupNavigationController()
{
NavigationController<ViewType>.Current.Clear();

////add known views
NavigationController<ViewType>.Current.Register(ViewType.About, new Uri("/Views/AboutView.xaml", UriKind.Relative));
NavigationController<ViewType>.Current.Register(ViewType.ErrorView, new Uri("/Views/ErrorView.xaml", UriKind.Relative));
NavigationController<ViewType>.Current.Register(ViewType.Home, new Uri("/MainView.xaml", UriKind.Relative));
NavigationController<ViewType>.Current.Register(ViewType.PropertyEditor, new Uri("/Views/PropertyView.xaml", UriKind.Relative));
NavigationController<ViewType>.Current.Register(ViewType.Security, new Uri("/Views/SecurityView.xaml", UriKind.Relative));
NavigationController<ViewType>.Current.Register(ViewType.SecurityHistoryView, new Uri("/Views/SecurityHistoryView.xaml", UriKind.Relative));
NavigationController<ViewType>.Current.Register(ViewType.Settings, new Uri("/Views/SettingsView.xaml", UriKind.Relative));
NavigationController<ViewType>.Current.Register(ViewType.Theory, new Uri("/Views/TheoryView.xaml", UriKind.Relative));
NavigationController<ViewType>.Current.Register(ViewType.Trade, new Uri("/Views/TradeView.xaml", UriKind.Relative));
}

After having made these registrations, navigation is quite easy anywhere in your application. It is as simple as calling like this:

public void NavigateToAbout()
{
//navigate to the About view...
NavigationController<ViewType>.Current.NavigateTo(ViewType.About);
}

You will never misspell or do anything wrong using this approach. All addresses are written in one place only (app.xaml) and you refer to them as an enum-value.


Download the improved version here: www.clauskonrad.net/download.ashx?id=13


Technorati Tags: ,

1 comment:

Anonymous said...

Why just views and not use cases?
I think application controller
-Use Case Controller
should "navigate" to a use case,
the use case should handle view composition and navigation and stuff which is not related to UI as well.

A button is bound to a use case

Nounda.dee@hotmail.com