将Castle.Windsor与MVVMLight Toolkit结合使用的实际示例 [英] Practical example of using Castle.Windsor with MVVMLight Toolkit
问题描述
我确实尝试过,但是找不到使用Castle IOC和MVVMLight的良好工作示例.可以动态地生成ViewModelBase视图模型的任何方式的指导.我正在尝试使用构造函数注入将viewmodel与数据源相关联,例如:
I have really tried but I cannot find a good working example of using the Castle IOC with MVVMLight. Any sort of guidance in the way to dynamically generate ViewModelBase viewmodels would be appreciated. I am trying to use Constructor Injection to associate the viewmodel with a data source, something like:
public class MainViewModel : ViewModelBase
{
...
public MainViewModel( ISomeSortofDataRepsoitory mysomesortofdata)
myrepo = mysomesortofdata; /// data items in an observable collection
}
,我希望ViewModelLocator
做类似的事情:
and I want the ViewModelLocator
to do something like:
public static void CreateMain()
{
if (_main == null)
{
...
_main = ioc.Resolve<MainViewModel>();
...
}
}
哎呀,没有骰子.有可行的例子吗?
alas, no dice. Any working examples?
推荐答案
您需要从NuGet安装CommonServiceLocator.WindsorAdapter软件包.然后在您的ViewModelLocator ctor
You need to install the CommonServiceLocator.WindsorAdapter package from NuGet. Then in your ViewModelLocator ctor
var container = new WindsorContainer();
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));
OR
完全删除ViewModelLocator类,并将所有Castle Windsor配置放入
Delete the ViewModelLocator class altogether, and put all your Castle Windsor configuration in
override void OnStartup(StartupEventArgs e)
在App.xaml.cs
in App.xaml.cs
这篇关于将Castle.Windsor与MVVMLight Toolkit结合使用的实际示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!