Practical example of using Castle.Windsor with MVVMLight Toolkit

ぃ、小莉子 提交于 2019-12-11 07:28:53

问题


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
}

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?


回答1:


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

Delete the ViewModelLocator class altogether, and put all your Castle Windsor configuration in

override void OnStartup(StartupEventArgs e)

in App.xaml.cs



来源:https://stackoverflow.com/questions/5709546/practical-example-of-using-castle-windsor-with-mvvmlight-toolkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!