Catel / Ninject:解析IUIVisualizerService时的Ninject.ActivationException

时间:2014-03-06 10:13:57

标签: c# ninject catel

当我尝试解析IUIVisualizerService时,Ninject.dll中出现了Ninject.ActivationException。

有人可以帮我吗?

此代码部分调用问题:

NinjectDependencyResolver resolver = new NinjectDependencyResolver();
            var item = resolver.GetService<IUIVisualizerService>();

解析器代码:

using System;
    using System.Collections.Generic;
    using Catel.Services;
    using Ninject;
    using NLog;

    namespace MS_Modell.Infrastructure
    {
        internal class NinjectDependencyResolver
        {
            private IKernel kernel;
            private Logger log = LogManager.GetCurrentClassLogger();

            public NinjectDependencyResolver()
            {
                try
                {
                    kernel = new StandardKernel();
                    kernel.Bind<IUIVisualizerService>().To<UIVisualizerService>();
                }
                catch (Exception ex)
                {
                    log.Fatal("NinjectDependencyResolver(): " + ex);
                    throw;
                }
            }

            public T GetService<T>()
            {
                try
                {
                    return kernel.TryGet<T>();
                }
                catch (Exception ex)
                {
                    log.Fatal("GetService<T>(): " + ex.Message);
                    throw;
                }
            }       
        }
    }

编辑:调用GetService后我得到一个null对象。但是没有提出异常。只能看到Visual Studio控制台输出上的消息:

在Ninject.dll中抛出了类型为“Ninject.ActivationException”的expcetion(第一次机会)。

编辑2:

Thx家伙为快速答案。 对于遇到同样问题的人来说,这是具体的解决方案:

在NinjectResolver中,您需要添加以下代码:

    kernel.Bind<IViewLocator>().To<ViewLocator>();
    kernel.Bind<IUIVisualizerService>().To<UIVisualizerService>().WithConstructorArgument("ViewLocator", GetService<IViewLocator>());

解决IUIVisualizerService:

TargetSelectorViewModel selector = new TargetSelectorViewModel();
                var item = resolver.GetService<IUIVisualizerService>();
                item.Register(typeof(TargetSelectorViewModel), typeof(TargetSelector));
                item.ShowDialog(selector);

1 个答案:

答案 0 :(得分:0)

UIVisualizerService需要依赖注入IViewLocator。确保Ninject也能解决这个问题。

https://github.com/Catel/Catel/blob/develop/src/Catel.MVVM/Catel.MVVM.NET40/Services/UIVisualizerService.cs#L54