WPF Caliburn IViewFactory找不到构造函数 - 'Castle.MicroKernel.Resolvers.DependencyResolverException'

时间:2014-09-19 10:24:24

标签: c# wpf dependency-injection castle-windsor caliburn.micro

我正在使用MVF应用程序(.NET 4.5),它使用MVVM和Caliburn来使用IViewFactory接口来引导视图。
我遇到了一个奇怪的问题,我的ViewModel中只有一个(QuestionRadioBtnViewModel)被初始化了。
在运行时我尝试初始化viewModel

var questionRadBtnVm = _viewFactory.CreateQuestionRadioBtnViewModel(answer.Text);

错误消息回来了:

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:

 public interface IViewFactory
 {
     QuestionRadioBtnViewModel CreateQuestionRadioBtnViewModel(string textValue);
 }

QuestionRadioBtnViewModel

 public QuestionRadioBtnViewModel(IEventAggregator eventAggregator, string stringValue)
 {
    _stringValue = stringValue;
    _eventAggregator = eventAggregator;
 }

Caliburn Bootstraper

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的所有其他构造函数都正常工作,数据传递没问题。 我一定错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:0)

自己找到。
 事实证明,不仅方法的签名必须匹配,而且还要传递参数的名称

public QuestionRadioBtnViewModel(IEventAggregator eventAggregator, string stringValue)

stringValue文字必须匹配。

public interface IViewFactory
 {
     QuestionRadioBtnViewModel CreateQuestionRadioBtnViewModel(string stringValue);
 }