开发者

Practical example of using Castle.Windsor with MVVMLight Toolkit

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜