Windows Phone应用程序中成分的根在哪里? [英] Where is the composition root in a Windows Phone app?
本文介绍了Windows Phone应用程序中成分的根在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是WP 7的新手。出于依赖关系注入的目的,我希望遵循使用WinForms应用程序获得的实践。因此,我想在组成根目录下构建我的应用程序图。
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?
推荐答案
我喜欢的模式如图案和图片建议的那样实践团队是:
- 将
ViewModelLocator
类添加到您的资源App.xaml
和x:Key = ViewModelLocator
,并为该类添加每个ViewModel的属性 - 在应用程序构造函数中配置依赖项(App.xaml中的
App()
.cs)并将容器实例提供给ViewModelLocator
(通过Application.Resources [ ViewModelLocator]
) - 通过将
DataContext = {Binding ViewModelPropertyName,Source = {StaticResource ViewModelLocator}}
$绑定到每个页面的ViewModel b $ b
- Add a
ViewModelLocator
class to your resourcesApp.xaml
with anx: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 theViewModelLocator
(viaApplication.Resources["ViewModelLocator"]
) - Bind the ViewModel to each page by assigning
DataContext="{Binding ViewModelPropertyName, Source={StaticResource ViewModelLocator}}"
有关详细信息,请参见 WP7指南有效的实施。
See the WP7 Guide for a working implementation.
这篇关于Windows Phone应用程序中成分的根在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文