在 WPF、Ninject 和 Caliburn.Micro 中单击打开新的 window

Open new window on click in WPF, Ninject and Caliburn.Micro

我正在尝试设置一个 WPF 应用程序,以便在单击菜单时调用新的 window,并将数据提供程序界面注入到新的视图模型中。

学习了许多教程并为 Caliburn 创建了 Bootstrapper,它是 ninject 的服务定位器和模块。到目前为止,主视图不需要 IDataProvider,但我想在点击事件上打开一个新的 window。

引导程序:

public class Bootstrapper : BootstrapperBase
    {
        public Bootstrapper()
        {
            Initialize();
        }

        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            DisplayRootViewFor<MainScreenViewModel>();
        }
    }

服务定位器和模块:

 public class ServiceLocator
    {
    private readonly IKernel _kernel;

        public ServiceLocator()
        {
            _kernel = new StandardKernel(new ServiceModule());
        }

        public MainScreenViewModel MainScreenViewModel => _kernel.Get<MainScreenViewModel>();

        public NewLayoutViewModel NewLayoutViewModel => _kernel.Get<NewLayoutViewModel>();
    }

 public class ServiceModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ISqlite>().To<Sqlite>();
            Bind<IDataProvider>().To<DataProvider>();
        }
    }

这就是我卡住的地方:

    public class MainScreenViewModel : Conductor<object>
    {
        private IWindowManager _windowManager;

        public MainScreenViewModel()
        {
            _windowManager = new WindowManager();
        }

        public void NewLayout()
        {
            _windowManager.ShowWindow(new NewLayoutViewModel());
        }
    }

因为 NewLayoutViewModel 需要 IDataProvider。

不确定,我错过了什么,但据我所知,Ninject 应该处理 NewLayoutViewModel 的这个 di。

YouTube.

上从 Tim Corey 那里找到了一个很好的解决方案

基本上答案是,如果你不坚持使用Ninjet,使用Caliburn.Micro的build-in DI解决方案"SimpleContainer"。