Where is the composition root in a Windows Phone app?

↘锁芯ラ 提交于 2020-01-24 07:45:45

问题


I'm new to WP 7. For purposes of dependency injection, I want to adhere to practices acquired doing WinForms apps. I therefore want to build my app graph at the composition root. What part of a regular WP 7 app source code can be viewed as the composition root?


回答1:


The pattern I prefer, as recommended by the Patterns & Practices team, is:

  • Add a ViewModelLocator class to your resources App.xaml with an x:Key="ViewModelLocator", and add to that class a property for each ViewModel type and lazy-instantiates the VM using the container.
  • Configure your dependencies in the application constructor (App() in App.xaml.cs) and provide the container instance to the ViewModelLocator (via Application.Resources["ViewModelLocator"])
  • Bind the ViewModel to each page by assigning DataContext="{Binding ViewModelPropertyName, Source={StaticResource ViewModelLocator}}"

See the WP7 Guide for a working implementation.



来源:https://stackoverflow.com/questions/7310162/where-is-the-composition-root-in-a-windows-phone-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!