DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Injecting ViewModels to Views using a custom ViewModelLocator in Windows Phone

Injecting ViewModels to Views using a custom ViewModelLocator in Windows Phone

Jevgeni Tšaikin user avatar by
Jevgeni Tšaikin
·
Nov. 20, 11 · Mobile Zone · Interview
Like (0)
Save
Tweet
5.86K Views

Join the DZone community and get the full member experience.

Join For Free

In one of my previous posts (IoC for Windows Phone: Ninject I have shown a simple IoC and DI tutorial using Ninject. In this tutorial will be based on previously developed code and extended that with a custom ViewModelLocator to bind ViewModels directly to Views. Hopefully, it will allow you to insert ViewModels with dependencies injected easier and faster for your Windows Phone or Silverlight applications.


As usual, without any introductions lets get directly to the development environment and to the previously created code. Before we begin, we need to remove one line of code from our View’s codebehind(constructor)

  
public class ViewModelLocator
{
    public NinjectSampleViewModel NinjectSampleVm
    {
        get { return IoCContainter.Get<NinjectSampleViewModel>(); }
    }
}

Adding ViewModelLocator as an Application Resource

Next step is to add this ViewModelLocator to the App.xaml as a Resource. It will give us an option to access it as a StaticResource from the Views. So add the following lines to App.xaml file:

<Application.Resources>
    <EugeneDotnetIoCSamples:ViewModelLocator x:Key="ViewModelLocator" />
</Application.Resources>

Adjusting the View

Finally, let’s open the View and modify that a little bit by adding the line bellow to phone:PhoneApplicationPage element:

DataContext="{Binding NinjectSampleVm, Source={StaticResource ViewModelLocator}}"
This line binds NinjectSampleVm to DataConext property of View using ViewModelLocator resource.

Results

The result should be similar to the result from the previous tutorial, but now the ViewModel is inserted via ViewModelLocator. I think this approach is suitable for a large application development, because it allows you to decouple ViewModel with dependencies injected to a single ViewModelLocator class to use it for testability(i.e Unit Tests). Also this approach significantly decreases the amount of code inside your application.

You can find the source code inside EugeneDotnetIoCSamples Project inside EugeneDotnetWPCodeSamples GitHub project:

eugenedotnet github windows phone project


Source: http://www.eugenedotnet.com/2011/11/injecting-viewmodels-to-views-using-a-custom-viewmodellocator/
Windows Phone

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing
  • Top 7 Automated Testing Trends of 2022
  • What Is HttpSession in Servlets?
  • Java Outsourcing, a Strong Business, and Management Approaches

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo