[英]How to register a class with a dependency injected constructor? (SimpleIoC)
我在項目中使用了MVVM Light,但不確定如何在構造函數中使用參數的ViewModelLocator類中注冊Viewmodel類。
我已經瀏覽了IoC上的文檔,但沒有看到與使用依賴項注入的構造函數注冊類有關的任何內容,即,該類帶有參數。
在我要注冊的類中,構造函數在其參數中采用一個列表,如下所示:
public ViewSubjectGradeViewModel(IEnumerable<ScoreModel> addedSubjectGradePairs)
但是,當我對ViewModel類進行導航時,會收到ActivationException,它詳細說明:
“ Microsoft.Practices.ServiceLocation.ActivationException未通過用戶代碼HResult = -2146233088處理:消息=無法注冊:在ViewSubjectGradeViewModel中找到多個構造函數,但未標記有PreferredConstructor。Source = GalaSoft.MvvmLight.Extras StackTrace:在GalaSoft.MvvmLight.Ioc.SimpleIoc中GalaSoft.MvvmLight.Ioc.SimpleIoc.GetPreferredConstructorInfo(IEnumerable`1 ConstructorInfos,Type resolveTo)在GalaSoft.MvvmLight.Ioc.SimpleIoc.Register [TClass](Boolean createInstanceImmediately)的GalaSoft.MvvmLight.Ioc.SimpleIoc.GetConstructorInfo(Type serviceType) LC_Points.LC_Points_WindowsPhone_XamlTypeInfo.XamlTypeInfoProvider.Activate_0_ViewModelLocator()處LC_Points.LC_Points_WindowsPhone_XamlTypeInfo.XamlUserType.ActivateInstance()處的LC_Points.ViewModel.ViewModelLocator..ctor()處的.RegisterTClass。
有誰知道如何解決此錯誤並指定“ PreferredConstructor”?
錯誤本身在我注冊ViewModel類的行上引發:
這是我的ViewModelLocator類,其中定義了VM的注冊:
namespace LC_Points.ViewModel
{
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// </summary>
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<ScoreModel>();
SimpleIoc.Default.Register<ViewSubjectGradeViewModel>();
}
public MainViewModel MainPage
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public ViewSubjectGradeViewModel ViewSubjectGradePage
{
get
{
return ServiceLocator.Current.GetInstance<ViewSubjectGradeViewModel>();
}
}
public ScoreModel ScoreProperty
{
get
{
return ServiceLocator.Current.GetInstance<ScoreModel>();
}
}
}
}
您不能像這樣單獨注冊界面。 您已經注冊了一個實現接口:
class YourImplement: IEnumerable
{
....
}
SimpleIoc.Default.Register<IEnumerable, YourImplement>();
或這個:
SimpleIoc.Default.Register<IEnumerable>(()=>new YourImplement());
布萊恩,這就是您要尋找的...
假設:
interface IMyInterface {...}
class MyClass : IMyInterface {...}
IEnumerable myEnumerable = new List<string>();
然后:
SimpleIoc.Default.Register<IMyInterface>( () => new MyClass(myEnumerable) );
晚會晚了,但是我認為您正在尋找的是使用[PreferredConstructor()]屬性裝飾您的構造函數。
我懷疑您的類“ ViewSubjectGradeViewModel”必須具有多個構造函數。 可能是無參數的構造函數,而您想要將其依賴項注入的構造函數如下:
public class ViewSubjectGradeViewModel
{
public ViewSubjectGradeViewModel()
{
//default ctor
}
[PreferredConstructor()] //add this attribute
public ViewSubjectGradeViewModel(IEnumerable<ScoreModel> addedSubjectGradePairs)
{
//dependency injected ctor
}
}
PreferredConstructorAttribute將告訴SimpleIoc將調用哪個構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.