0

私のNinjectコンストラクトに問題があります。誰かが私が間違っているところを教えてくれるかもしれません..

わかりました..ここに私が持っているモジュールがあります:

public class WebPageModule:NinjectModule
        {
            public override void Load()
            {
                Bind<TranscriptPageMediaWidgetViewModelForWebPage>().ToSelf().InSingletonScope();
                Bind<TranscriptPageTranscriptWidgetViewModelForWebPage>().ToSelf().InSingletonScope();
                Bind<WebPageTranscriptProvider>().ToSelf().InSingletonScope();

                Bind<ITranscriptProvider>().To<WebPageTranscriptProvider>().WhenInjectedInto<TranscriptPageTranscriptWidgetViewModelForWebPage>();

                //Bind<ITranscriptProvider>().To<WebPageTranscriptProvider>();
                Bind<ITranscriptRendererWidget>().To<TranscriptPageTranscriptWidgetViewModelForWebPage>();
                Bind<IMediaRendererWidget>().To<TranscriptPageMediaWidgetViewModelForWebPage>();
            }
        }

次に、NinjectWebCommons.cs に次のものがあります。

private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel(new WebPageModule(),new TweeterModule(), new BookmarkModule());
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            kernel.Settings.AllowNullInjection = true;//http://stackoverflow.com/questions/10517962/using-default-parameter-values-with-ninject-3-0

            RegisterServices(kernel);
            return kernel;
        }

次に、プロパティ インジェクションを使用します: https://github.com/ninject/ninject/wiki/Injection-Patterns

私の「パブリッククラスTranscriptPageTranscriptWidgetViewModelForWebPage:ITranscriptRendererWidget」で

ここにあります:

 [Inject]
        public ITranscriptProvider TranscriptProvider
        {
            get { return _transcriptProvider; }
            set { _transcriptProvider = value; }
        }

しかし、コンストラクターに入って _transcriptProvider を使用しようとすると、NULL になります。

 public TranscriptPageTranscriptWidgetViewModelForWebPage(string dataEndpoint, string focusCue)
        {
            InitParentInterfaceProperties();
            Transcript = _transcriptProvider.GetTranscript(new Uri(dataEndpoint));
            FocusCue = focusCue.Replace("*", "").ToLower();
        }

私が間違っていることは何ですか?ありがとう!アル

4

1 に答える 1

0

コンストラクター内のプロパティにアクセスしようとしているようです。

.NETのオブジェクト作成セマンティクスは、これを単純に機能させることができないようなものです(これは、オプションの依存関係を実際に処理していない限り、コンストラクターインジェクションで物事を達成するために非常に懸命に努力する多くの理由の1つです)

于 2012-09-21T22:53:26.860 に答える