プロジェクトで MVVM Light を使用していますが、コンストラクターでパラメーターを受け取る ViewModelLocator クラスに Viewmodel クラスを登録する方法がわかりません。
IoCのドキュメントを調べましたが、依存関係が挿入されたコンストラクター (つまり、パラメーターを取るクラス) を使用してクラスを登録することに関連するものは何も表示されません。
登録したいクラスでは、コンストラクターは次のようなパラメーターでリストを受け取ります。
public ViewSubjectGradeViewModel(IEnumerable<ScoreModel> addedSubjectGradePairs)
しかし、ViewModel クラスへのナビゲーションを実行すると、ActivationException が発生します。詳細は次のとおりです。
「Microsoft.Practices.ServiceLocation.ActivationException は、ユーザー コード HResult=-2146233088 によって処理されませんでした: 登録できません: 複数のコンストラクターが ViewSubjectGradeViewModel に見つかりましたが、PreferredConstructor でマークされたコンストラクターはありませんでした。ソース = GalaSoft.MvvmLight.Extras StackTrace: at GalaSoft.MvvmLight.Ioc.SimpleIoc .GetPreferredConstructorInfo(IEnumerable`1 constructorInfos, Type resolveTo) at GalaSoft.MvvmLight.Ioc.SimpleIoc.GetConstructorInfo(Type serviceType) at GalaSoft.MvvmLight.Ioc.SimpleIoc.Register[TClass](Boolean createInstanceImmediately) at GalaSoft.MvvmLight.Ioc.SimpleIoc .RegisterTClass at LC_Points.ViewModel.ViewModelLocator..ctor() at LC_Points.LC_Points_WindowsPhone_XamlTypeInfo.XamlTypeInfoProvider.Activate_0_ViewModelLocator() at LC_Points.LC_Points_WindowsPhone_XamlTypeInfo.XamlUserType.ActivateInstance() InnerException: "
このエラーを解決して「PreferredConstructor」を指定する方法を知っている人はいますか?
エラー自体は、ViewModel クラスを登録する行でスローされます。
これは、VM の登録が定義されている私の ViewModelLocator クラスです。
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>();
}
}
}
}