首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >'Castle.MicroKernel.Resolvers.DependencyResolverException‘IViewFactory找不到构造器-IViewFactory

'Castle.MicroKernel.Resolvers.DependencyResolverException‘IViewFactory找不到构造器-IViewFactory
EN

Stack Overflow用户
提问于 2014-09-19 10:24:23
回答 1查看 904关注 0票数 0

我正在开发WPF应用程序(.NET 4.5),它使用MVVM和Caliburn通过IViewFactory接口引导视图。

我遇到了一个特殊的问题,除了一个(QuestionRadioBtnViewModel)的ViewModels正在被初始化。

在运行时,当我试图初始化viewModel时

代码语言:javascript
复制
var questionRadBtnVm = _viewFactory.CreateQuestionRadioBtnViewModel(answer.Text);

返回错误消息:

代码语言:javascript
复制
A first chance exception of type 'Castle.MicroKernel.Resolvers.DependencyResolverException' occurred in Castle.Windsor.dll

Additional information: Could not resolve non-optional dependency for 'Corp.Conveyancing.Desktop.ViewModels.Question.QuestionRadioBtnViewModel' (Corp.Conveyancing.Desktop.ViewModels.Question.QuestionRadioBtnViewModel). Parameter 'stringValue' type 'System.String'

但是,方法签名与构造函数匹配得很好。

IViewFactory:

代码语言:javascript
复制
 public interface IViewFactory
 {
     QuestionRadioBtnViewModel CreateQuestionRadioBtnViewModel(string textValue);
 }

QuestionRadioBtnViewModel

代码语言:javascript
复制
 public QuestionRadioBtnViewModel(IEventAggregator eventAggregator, string stringValue)
 {
    _stringValue = stringValue;
    _eventAggregator = eventAggregator;
 }

卡利伯恩助力器

代码语言:javascript
复制
public class ReactiveBootstrapper : BootstrapperBase
{
    public ReactiveBootstrapper()
    {
        Log.Info("Starting bootstrapper");
        Start();
    }

    protected override void OnStartup(object sender, StartupEventArgs e)
    {
        DisplayRootViewFor(typeof(MainViewModel));
    }

    protected override void BuildUp(object instance)
    {
        Container.BuildUp(instance);
    }

    protected override void Configure()
    {
        Container = new ApplicationContainer();

        Container.RegisterViewModels(typeof(MainViewModel));

        SetXamlLanguage();
        Container.Install(FromAssembly.Containing<IObjectModelFactory>());
        Container.AddFacility<LoggingFacility>(f => f.UseLog4Net(Assembly.GetEntryAssembly().GetName().Name + ".exe.log4net"));
        Container.AddFacility<TypedFactoryFacility>();
        Container.Register(Component.For<IViewFactory>().AsFactory());
        Container.Register(Component.For<IServerOperations>().ImplementedBy<ServerOperations>());
        Container.Register(Component.For<IQuestionControlFactory>().ImplementedBy<QuestionControlFactory>());
        Container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero);

        RegisterWcfServices();

        Container.Register(Component.For<IQasManager>().ImplementedBy<QasWebManager>().DependsOn(Dependency.OnValue("url", Settings.Default.QasUrl)));
    }

    protected override IEnumerable<object> GetAllInstances(Type service)
    {
        return Container.ResolveAll(service).Cast<object>();
    }

    protected override IEnumerable<Assembly> SelectAssemblies()
    {
        return new[] {
            Assembly.GetExecutingAssembly(),
            typeof(MainViewModel).Assembly,
            typeof(MessageViewModel).Assembly
        };
    }

    protected override object GetInstance(Type service, string key)
    {
        if (string.IsNullOrWhiteSpace(key))
        {
            return Container.Resolve(service);
        }
        return Container.Resolve(key, service);
    }
}

所有其他正在使用IViewFactory的构造函数都很好地工作,并且数据被传递没有问题。我一定是漏掉了什么明显的东西?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-19 10:54:07

我自己找到的。

结果表明,不仅方法的签名必须匹配,而且传递的参数的名称也必须匹配。

代码语言:javascript
复制
public QuestionRadioBtnViewModel(IEventAggregator eventAggregator, string stringValue)

stringValue文本必须匹配。

代码语言:javascript
复制
public interface IViewFactory
 {
     QuestionRadioBtnViewModel CreateQuestionRadioBtnViewModel(string stringValue);
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25931722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档