2011-04-18 63 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