Array

Connecting the View and the View Model

Must Read

Canon PowerShot G12 The Lens and Focal Lengths

The technology and engineering that goes into your camera is a marvel, but it isn’t worth a darn if...

A Bad Credit Home Equity Loan Can Help You Take Control Of Your Finances

A bad credit home equity loan can help you get the cash you need if you are a homeowner...

Nikon D7000, The Portrait Picture Control for Better Skin tones

As long as we are talking about picture controls for portraits, there is another control on your D7000 that...

Canon EOS 60D, Rule of Thirds

As mentioned in previous chapters, it’s usually a good idea to avoid placing your subject directly in the center...

Using Guidelines for Online Mortgage Rate Tables and Quote Solutions

Systematically you should be watching the rates and move in most opportune moment If you want to grab the...

Using IronPython for Application Testing

Understanding Why You Want to Use IronPython for Testing Every testing technique you’ve ever used has some drawback. For example,...
Admin
test

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 (http://go.microsoft.com/fwlink/?LinkId=205602).

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.

XAML
<Application
x:Class=”TailSpin.PhoneClient.App”

xmlns:viewmodels=
“clr-namespace:TailSpin.PhoneClient.ViewModels”
… >
<!–Application Resources–>
<Application.Resources>

<viewmodels:ViewModelLocator x:Key=”ViewModelLocator”/>
</Application.Resources>

</Application>

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.

XAML
<phone:PhoneApplicationPage
x:Class=”
TailSpin.PhoneClient.Views.SurveyList.SurveyListView”

DataContext=
“{Binding Source={StaticResource ViewModelLocator},
Path=SurveyListViewModel}”

>

</phone:PhoneApplicationPage>

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.

C#
public class ViewModelLocator : IDisposable
{
private readonly ContainerLocator containerLocator;

public SurveyListViewModel SurveyListViewModel
{
get
{
return this.containerLocator.Container
.Resolve<SurveyListViewModel>();
}
}
}

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.

C#
public TakeSurveyViewModel TakeSurveyViewModel
{
get
{
var templateViewModel =
this.SurveyListViewModel.SelectedSurveyTemplate;
var vm = new TakeSurveyViewModel(
this.containerLocator.Container
.Resolve<INavigationService>())
{
SurveyStoreLocator = this.containerLocator.Container
.Resolve<ISurveyStoreLocator>(),
LocationService = this.containerLocator.Container
.Resolve<ILocationService>(),
TemplateViewModel = templateViewModel,
SurveyAnswer = this.containerLocator.Container
.Resolve<ISurveyStoreLocator>().GetStore()
.GetCurrentAnswerForTemplate(templateViewModel.Template)
?? templateViewModel.Template.CreateSurveyAnswers(
this.containerLocator.Container
.Resolve<ILocationService>())
};
vm.Initialize();
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

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...

What are 7 things poor people do that the rich don’t?

1. poor people watch TV in which people read books how many hours you spend in front of the TV and when was the...

Top 18 best small business ideas for beginners starting

A small business can be frightening and requires plenty of careful planning there are many small business ideas which can be beneficial as well...

Summer that makes you happy

We saw were already here I've been thinking about some of the things. I used to do with my husband even though he had...

4 Point to helpful tips specifically for caregivers

What you need to take a vacation. I know it sounds impossible creative and try to make it work for you almost everyone needs...

More Articles Like This