Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:05:04+00:00 2026-06-15T19:05:04+00:00

I want to be able to inject ViewModels into the Views in my Windows

  • 0

I want to be able to inject ViewModels into the Views in my Windows Phone 8 application using Autofac as my IoC container. How do I go about doing this? I have looked at the Caliburn.Micro framework, but I’d like to use something simpler.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:05:05+00:00Added an answer on June 15, 2026 at 7:05 pm

    Precisely for this purpose I have created a small demo application. It defines a ViewModelLocator class:

    public class ViewModelLocator
    {
        private readonly IContainer container;
    
        public ViewModelLocator()
        {
            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterType<MainViewModel>();
            containerBuilder.RegisterType<ItemViewModel>();
    
            this.container = containerBuilder.Build();
        }
    
        public MainViewModel MainViewModel
        {
            get
            {
                return this.container.Resolve<MainViewModel>();
            }
        }
    
        public ItemViewModel ItemViewModel
        {
            get
            {
                return this.container.Resolve<ItemViewModel>();
            }
        }
    }
    

    To use this class from your views, you have to add it to your application’s resources. You do this by modifying the Application.Resources section in App.xaml:

    <Application.Resources>
        <local:ViewModelLocator xmlns:local="clr-namespace:AutofacWP8DependencyInjectionDemo" x:Key="ViewModelLocator"/>
    </Application.Resources>
    

    Now you will be able to inject the view model in the view. Just have the view point to the DataContext. To reference the MainViewModel as the DataContext simply add the following to your view:

    DataContext="{Binding Path=MainViewModel, Source={StaticResource ViewModelLocator}}"
    

    You can see that it sets the DataContext to the MainViewModel property of the ViewModelLocator class, which uses Autofac to create the MainViewModel instance using dependency injection.

    You can find the source here: https://github.com/ErikSchierboom/autofacwp8dependencyinjectiondemo.git

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to inject model state into a service. Service public
I'm using spring with my application, and I'm able to inject some properties from
I'm using an MVC setup and I'm trying to inject javascript into my views
I want to be able to inject a .dll into a running JVM process.
I'm using LaTeX and BibTeX for an article, and I want to able to
I want to be able to prevent regular shutdown on windows 7. I found
I have a DLL that I inject into another process but I want to
I want to use @AutoWired to inject a non-managed bean configured with @Component into
Can ASMX have an IOC like WCF? Basically I want to be able to
I want to be able to use ninject to inject a different type of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.