C# 使用Caliburn.Micro同时显示两个WPF窗口

C# 使用Caliburn.Micro同时显示两个WPF窗口,c#,wpf,mvvm,ninject,caliburn.micro,C#,Wpf,Mvvm,Ninject,Caliburn.micro,我需要在启动时在单独的窗口中同时显示两个WPF控件。父窗口的类型相同,用户控件(和父窗口)在单独的程序集中定义,该程序集仅由主体项目引用。我正在使用Caliburn.Micro作为MVVM框架,并为IoC使用Ninject。如何做到这一点 所有视图模型都是从PropertyChangedBase派生的。我已经设置了AppBootstrapper来定义Caliburn.Micro标准绑定,例如WindowManager: _kernel.Bind<IControl1>().To&l

我需要在启动时在单独的窗口中同时显示两个WPF控件。父窗口的类型相同,用户控件(和父窗口)在单独的程序集中定义,该程序集仅由主体项目引用。我正在使用Caliburn.Micro作为MVVM框架,并为IoC使用Ninject。如何做到这一点

所有视图模型都是从PropertyChangedBase派生的。我已经设置了AppBootstrapper来定义Caliburn.Micro标准绑定,例如WindowManager:

  _kernel.Bind<IControl1>().To<Control1ViewModel>().InSingletonScope();
  _kernel.Bind<IControl2>().To<Control2ViewModel>().InSingletonScope();
  _kernel.Bind<IParentWindow>().To<ParentWindowViewModel>();
\u kernel.Bind()To().InSingletonScope();
_kernel.Bind().To().InSingletonScope();
_kernel.Bind().To();
并为创建Control1的OnStartup创建了覆盖:

DisplayRootViewFor<IWindow1>();
DisplayRootViewFor();
用户控件作为窗口上下文提供给ContentControl中的父窗口,如下所示:

<ContentControl x:Name="WindowView"
     HorizontalAlignment="Stretch"
     VerticalAlignment="Stretch"
     cal:View.Context="{Binding ViewContext}"
     cal:View.Model="{Binding WindowContent}" />

最后,我还提供了对SelectAssembly的覆盖,以便Caliburn.Micro可以在dll中找到视图和视图模型:

protected override IEnumerable<Assembly> SelectAssemblies()
{
    var assemblies = base.SelectAssemblies().ToList();
    assemblies.Add(typeof(IControl1).GetTypeInfo().Assembly);
    return assemblies;
}
受保护的重写IEnumerable SelectAssemblys()
{
var assemblies=base.SelectAssemblies().ToList();
Add(typeof(IControl1.GetTypeInfo().Assembly);
返回组件;
}
我尝试了几种可能的解决方案,但没有一种有效:

  • 从Window1视图模型的构造函数中打开Window2(使用WindowManager.ShowWindow)。但是,这只打开了Window2,从未打开过Window1。无论如何,这可能不是个好主意

  • 在AppBootstrapper.OnStartup中创建一个窗口,并使用App.xaml StartupUri创建另一个窗口,但是这不允许我在通用父窗口中包含用户控件。我所能做的就是打开一个空的父窗口

  • 在界面上调用DisplayRootViewFor(),以便在启动时打开每个窗口。问题是无法设置窗口内容,因此无法获得自定义父窗口,而只能获得Caliburn.Micro提供的默认窗口

  • 我是这样做的:

    在AppBootstrapper.cs中,首先创建父窗口的实例,而不是调用DisplayRootViewFor:

    var parentWindow = _kernel.Get<IParentWindow>();
    
    var parentWindow=\u kernel.Get();
    
    为父窗口上下文提供用户控件:

    parentWindow = _kernel.Get<IControl1>();
    
    parentWindow=\u kernel.Get();
    
    使用WindowManager.ShowWindow打开窗口:

    _kernel.Get<IWindowManager>().ShowWindow(parentWindow, null, windowSettings);
    
    \u kernel.Get().ShowWindow(parentWindow,null,windowSettings);
    
    只需对第二个窗口重复此过程:

    var window2 = _kernel.Get<IParentWindow>();
    window2.WindowContent = _kernel.Get<IControl2>() ;
    _kernel.Get<IWindowManager>().ShowWindow(window2, null, windowSettings);
    
    var window2=_kernel.Get();
    window2.WindowContent=_kernel.Get();
    _kernel.Get().ShowWindow(window2,null,windowSettings);
    
    这将使用WPF中的Caliburn.Micro创建两个窗口,其中包含在外部程序集中定义的用户控件