2014-09-19 37 views
0

我正在使用MVVM的WPF應用程序(.NET 4.5)和使用IViewFactory接口引導視圖的Caliburn
我遇到了一個奇怪的問題,其中除了一個(QuestionRadioBtnViewModel)我的ViewModels被初始化。
在運行時,當我試圖初始化視圖模型WPF Caliburn IViewFactory找不到構造函數 - 'Castle.MicroKernel.Resolvers.DependencyResolverException'

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; 
} 

卡利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做工精細和數據被傳遞沒有問題的其他構造。 我必須在這裏丟失一些明顯的東西嗎?

回答

0

自己找到了。
原來不僅方法的簽名必須匹配,而且傳遞也是參數的名稱

public QuestionRadioBtnViewModel(IEventAggregator eventAggregator, string stringValue) 

stringValue文本必須匹配。

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