2011-04-18 59 views
1

我真的尝试过了,但是我找不到使用MVCMLight的Castle IOC的好工作示例。将不胜感激任何类型的动态生成ViewModelBase视图模型的指导。我试图用构造函数注入到视图模型与数据源相关联,是这样的:使用Castle.Windsor和MVVMLight工具箱的实际例子

public class MainViewModel : ViewModelBase 
{ 
    ... 
    public MainViewModel(ISomeSortofDataRepsoitory mysomesortofdata) 
    myrepo = mysomesortofdata; /// data items in an observable collection 
} 

,我想ViewModelLocator做这样的事情:

public static void CreateMain() 
{ 
    if (_main == null) 
    { 
     ... 
     _main = ioc.Resolve<MainViewModel>(); 
     ... 
    } 
} 

唉,没有骰子。任何工作示例?

+0

好吧,我去这种工作,但它不是Blendable。它从容器生成视图模型并根据需要正确注入数据存储库,但容器本身没有在设计模式下实例化,因此没有数据可查看。我必须制作一个虚拟viewmodel来布局表单吗? – Allen 2011-04-19 15:40:54

+0

你能提供一些你做过的细节吗?我正在和MvvmLight一起玩Windsor,只是不明白这一点:( – ABR 2011-11-29 18:18:51

回答

2

您需要从NuGet安装CommonServiceLocator.WindsorAdapter软件包。然后在你的ViewModelLocator男星

var container = new WindsorContainer(); 
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container)); 

OR

完全删除ViewModelLocator类,并把所有的温莎城堡配置

override void OnStartup(StartupEventArgs e) 

在App.xaml.cs