当我试图解决 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 后我得到一个空对象。但是没有引发异常。在 Visual Studio 的控制台输出上只能看到一条消息:
在 Ninject.dll 中引发了类型为“Ninject.ActivationException”的异常(第一次机会)。
编辑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);