使用NInject在wpf应用程序中注册依赖项Resolver类

时间:2015-01-20 23:00:54

标签: c# .net wpf dependency-injection ninject

我有一个wpf应用程序,我想在其中使用NInject作为依赖注入器

所以我创建了Dependency Resolver类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject;
using Ninject.Parameters;
using Ninject.Syntax;

namespace NinjectWithWPF
{
   public class DependencyResolution 
    {
       public IKernel kernel;

       public DependencyResolution()
       {
           kernel = new StandardKernel();
           AddBindings();
       }

       private void AddBindings()
       {
           kernel.Bind<Icalcul>().To<CalculSimple>();
       }
       public object GetService(Type serviceType)
       {
           return kernel.TryGet(serviceType);
       }

       public IEnumerable<Object> GetServices(Type serviceType)
       {
           return kernel.GetAll(serviceType);
       }
    }
}

我需要知道如何在App.xaml

中的依赖解析器类中注册它
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Ninject;
using Ninject.Parameters;
using Ninject.Syntax;

namespace NinjectWithWPF
{
    /// <summary>
    /// Logique d'interaction pour App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {


        }
    }
}

所以我需要知道

  1. 这样做的正确方法是什么?
  2. 我可以在哪个配置文件中注册依赖项Resolver Class?

0 个答案:

没有答案