我正在开发WPF应用程序(.NET 4.5),它使用MVVM和Caliburn通过IViewFactory接口引导视图。
我遇到了一个特殊的问题,除了一个(QuestionRadioBtnViewModel)的ViewModels正在被初始化。
在运行时,当我试图初始化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;
}卡利伯恩助力器
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的构造函数都很好地工作,并且数据被传递没有问题。我一定是漏掉了什么明显的东西?
发布于 2014-09-19 10:54:07
我自己找到的。
结果表明,不仅方法的签名必须匹配,而且传递的参数的名称也必须匹配。
public QuestionRadioBtnViewModel(IEventAggregator eventAggregator, string stringValue)stringValue文本必须匹配。
public interface IViewFactory
{
QuestionRadioBtnViewModel CreateQuestionRadioBtnViewModel(string stringValue);
}https://stackoverflow.com/questions/25931722
复制相似问题