我创建了一个名为 ShellView 的窗口,它将加载或处理两个名为 AView 和 BView 的用户控件。
我已经有了这个设置:
Bootstrapper.cs
public class Bootstrapper : BootstrapperBase
{
private SimpleContainer _container = new SimpleContainer();
public Bootstrapper()
{
Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
base.OnStartup(sender, e);
DisplayRootViewFor<ShellViewModel>();
}
protected override void Configure()
{
_container.Singleton<IWindowManager, WindowManager>();
_container.Singleton<IEventAggregator, EventAggregator>();
_container.RegisterPerRequest(typeof(ShellViewModel), null, typeof(ShellViewModel));
}
protected override object GetInstance(Type serviceType, string key)
{
return _container.GetInstance(serviceType, key);
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return _container.GetAllInstances(serviceType);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
}
AViewModel.cs
public class AViewModel : Screen
{
private IEventAggregator _eventAggregator;
public AViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator.
}
}
这就是问题,如何在ShellViewModel中传入事件聚合器的实例?
ShellView.cs
public class ShellViewModel : Conductor<Screen>
{
public ShellViewModel()
{
// Should I create an instance and pass it in? If not, how?
ActivateItem(new AViewModel(?));
}
}
答案 0 :(得分:1)
Caliburn.Micro有一个名为IoC
的静态类。你应该这样做:
ActivateItem(IoC.Get<AViewModel>());
如果有意义,你可以做这样的事情:
public class ShellViewModel : Conductor<Screen>
{
private IEventAggregator _eventAggregator;
public ShellViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
ActivateItem(new AViewModel(_eventAggregator));
}
}