使用Castle.Windsor和MVVMLight Toolkit的实际示例

时间:2011-04-18 22:16:49

标签: mvvm-light

我已经尝试了但是我找不到使用Castle IOC和MVVMLight的一个很好的工作示例。在动态生成ViewModelBase视图模型的方式中的任何类型的指导将不胜感激。我试图使用构造函数注入将viewmodel与数据源相关联,如:

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>();
         ...
     }
}
唉,没有骰子。有什么实际的例子吗?

1 个答案:

答案 0 :(得分:2)

您需要从NuGet安装CommonServiceLocator.WindsorAdapter包。然后在ViewModelLocator ctor中

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

OR

完全删除ViewModelLocator类,并将所有Castle Windsor配置放入

override void OnStartup(StartupEventArgs e)

在App.xaml.cs