Connecting the View and the View Model

Must Read


Now is a good time to walk through the code that implements the MVVM pattern in the Windows Phone 7 application in more detail. As you go through this section, you may want to download the Microsoft Visual Studio® development system solution for the Windows Phone 7 Tailspin Surveys application; it is available on the Windows Phone 7 Developer Guide community site on CodePlex (

There are several ways to connect the view model to the view, including direct relations and data template relations. The developers at Tailspin chose to use a view model locator because this approach means that the application has a single class that is responsible for hooking up views to view models. This still leaves developers free to choose to manually perform the hookup within the view model locator, or by using a dependency injection container.

Inside the Implementation

The mobile client application uses a view model locator to connect view models to views. The following code example from the App.xaml file shows how the view model locator class is identified and made available to the application. The application declares the TailSpin. PhoneClient.ViewModels ViewModelLocator class in the <Application. Resources> section of the App.xaml file.


… >
<!–Application Resources–>

<viewmodels:ViewModelLocator x:Key=”ViewModelLocator”/>


The following code example from the SurveyListView.xaml file shows how a view can then reference the  ViewModelLocator class as a static resource in its data context bindings.


“{Binding Source={StaticResource ViewModelLocator},



The Path attribute identifies the property of the ViewModel Locator instance that returns the view model associated with the current page.

The following code example shows the parts of the ViewModel Locator class that return the SurveyListViewModel instance.

public class ViewModelLocator : IDisposable
private readonly ContainerLocator containerLocator;

public SurveyListViewModel SurveyListViewModel
return this.containerLocator.Container

Notice how the Tailspin phone client uses the Funq dependency injection container to instantiate the view model.

The following code example shows the parts of the ViewModel Locator class that return the TakeSurveyViewModel instance when the user selects a survey from the survey list. In this example, the view model is instantiated with details of the currently selected survey so that the application can display the survey questions on the Take SurveyView page.

public TakeSurveyViewModel TakeSurveyViewModel
var templateViewModel =
var vm = new TakeSurveyViewModel(
SurveyStoreLocator = this.containerLocator.Container
LocationService = this.containerLocator.Container
TemplateViewModel = templateViewModel,
SurveyAnswer = this.containerLocator.Container
?? templateViewModel.Template.CreateSurveyAnswers(
return vm;

Note: The view locator is also responsible for instantiating the store and passing it to the view model, which in turn passes it on to the model.

Previous articleThe Premise
Next articleTesting the Application
- Advertisement -

Latest News

Digital Marketing for Beginners

Digital marketing for starter, Let to basic learning about connecting with your audience in the right place at the...
- Advertisement -

More Articles Like This

- Advertisement -