1

プロジェクトで 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>();
            }
        }

    }
}
4

3 に答える 3

4

このようにインターフェースだけを登録することはできません。インターフェイスを実装して登録しました:

class YourImplement: IEnumerable
{
 ....
}

SimpleIoc.Default.Register<IEnumerable, YourImplement>(); 

またはこれ:

SimpleIoc.Default.Register<IEnumerable>(()=>new YourImplement());
于 2015-05-23T12:29:55.433 に答える
2

ブライアン、これはあなたが探しているものですか...

仮定:

interface IMyInterface {...}
class MyClass : IMyInterface {...}
IEnumerable myEnumerable = new List<string>();

それで:

SimpleIoc.Default.Register<IMyInterface>( () => new MyClass(myEnumerable) );
于 2016-05-04T18:22:47.447 に答える