我试图使用PRISM和统一来实现我的第一个应用程序。因此,我尝试将我的应用程序分成几个模块。
在我的模块中,我有相关的视图以及视图模型。
目前,我实例化了视图模型,并在视图代码后面设置了视图的数据文本:
public partial class View : UserControl
{
public View(IViewModel vm)
{
InitializeComponent();
this.DataContext = vm;
}
}我的模型是使用我的视图中的统一容器-模型ctor实例化的。
public ViewModel(IEventAggregator eventAggregator, IUnityContainer container)
{
_eventAggregator = eventAggregator;
_model = container.Resolve<Model>();
this._model.PropertyChanged += new PropertyChangedEventHandler(OnModelPropertyChanged);
}在使用统一容器之前,我通过ViewModels构造函数通过依赖注入注入了模型。
但这似乎行不通。我尝试了以下几种方法:
public ViewModel(IEventAggregator eventAggregator, Model model)
{
_eventAggregator = eventAggregator;
_model = model
this._model.PropertyChanged += new PropertyChangedEventHandler(OnModelPropertyChanged);
}这个实现给了我一个异常,而且我也无法找到如何设置容器,以便模型注入以我尝试的方式工作。
我想要做的是在模块类中实例化我的模型。然后我想把它注入到我的视图模型中。
所以我的问题是:
发布于 2014-11-21 06:31:33
看看UI组合QuickStart的棱镜5 (http://msdn.microsoft.com/en-us/library/gg430879(v=pandp.40).aspx。它能做你想做的事。
1) Bootstrapper中的注册模块:
moduleCatalog.AddModule(typeof(EmployeeModule.ModuleInit));2)在模块实现中注册模型类型(如果您的模型是共享的,则在引导程序中)
this.container.RegisterType<IEmployeeDataService, EmployeeDataService>();3)通过构造函数将模型注入视图模型。
public EmployeeListViewModel(IEmployeeDataService dataService, IEventAggregator eventAggregator) { }https://stackoverflow.com/questions/27053685
复制相似问题