2012-12-27 11 views
0

我已經下載並運行該示例,並瞭解大多數情況,但我無法弄清楚CategoryEditorViewModel類從何處獲取構造函數參數。它是「ICategoryRepository categoryRepository」,我已經搜索了引用,但仍然無法理解在創建視圖模型時如何以及在哪裏獲取paremeter。 我希望有人能幫我解答。MVVM Light Apuntas Notas類別:EditorViewModel構造函數

回答

1

我有同樣的問題。

至於什麼我從線程想通了,像

Constructors with multiple parameters

以及更多的是,在Apuntas NOTAS項目在bootstrap.cs文件,接口ICategoryRepository與類註冊CategoryReporitory。

因此,當CategoryEditor嘗試通過

 public CategoryEditorViewModel CategoryEditor 
    { 
     get { return _bootStrapper.Container.Resolve<CategoryEditorViewModel>(); } 
    } 

和實例解決ViewModelLocator.cs一個實例不存在的財產,它試圖創建該類CategoryEditorViewModel的對象,其僅希望構造ICategoryRespository接口。

正如我前面提到的,我們在bootstrap.cs文件中使用CategoryRepository類註冊了此接口。因此它創建了一個CategoryRepository對象並將其傳遞給視圖模型構造函數。

希望這會消除你的懷疑。

哦,如果你想知道,如果你有多個構造會發生什麼,你可以註冊一個,你喜歡在bootstrap.cs喜歡的東西

Container.RegisterType<CategoryEditorViewModel>(new InjectionConstructor(typeof(ICategoryRepository), 5)); 

註冊,現在,如果你有另一個構造中CategoryEditorViewModel另外期待一個int,將被調用,而不是之前那個。 (傳遞一個5的int值是非常愚蠢的,但希望你得到的圖片,你可以給你的首選構造函數的參數類型,並確保它們也註冊在引導容器中)