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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Precisely for this purpose I have created a small demo application. It defines a
ViewModelLocatorclass: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:
Now you will be able to inject the view model in the view. Just have the view point to the
DataContext. To reference theMainViewModelas theDataContextsimply add the following to your view:You can see that it sets the
DataContextto theMainViewModelproperty of theViewModelLocatorclass, which uses Autofac to create theMainViewModelinstance using dependency injection.You can find the source here: https://github.com/ErikSchierboom/autofacwp8dependencyinjectiondemo.git