繁体   English   中英

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

[英]Display two WPF windows simultaneously using Caliburn.Micro

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

所有视图模型均来自PropertyChangedBase。 我已经设置了AppBootstrapper来定义Caliburn.Micro标准绑定,例如WindowManager:

  _kernel.Bind<IControl1>().To<Control1ViewModel>().InSingletonScope();
  _kernel.Bind<IControl2>().To<Control2ViewModel>().InSingletonScope();
  _kernel.Bind<IParentWindow>().To<ParentWindowViewModel>();

并为创建Control1的OnStartup创建了替代:

DisplayRootViewFor<IWindow1>();

用户控件作为窗口上下文提供给ContentControl中的父窗口,如下所示:

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

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

protected override IEnumerable<Assembly> SelectAssemblies()
{
    var assemblies = base.SelectAssemblies().ToList();
    assemblies.Add(typeof(IControl1).GetTypeInfo().Assembly);
    return assemblies;
}

我尝试了几种可能的解决方案,但都没有成功:

  1. 从Window1视图模型的构造函数中打开Window2(使用WindowManager.ShowWindow)。 但是,这仅打开Window2,而从未打开Window1。 可能不是一个好主意。

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

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

这是我的做法:

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

var parentWindow = _kernel.Get<IParentWindow>();

向父窗口上下文提供用户控件:

parentWindow = _kernel.Get<IControl1>();

使用WindowManager.ShowWindow打开窗口:

_kernel.Get<IWindowManager>().ShowWindow(parentWindow, null, windowSettings);

只需对第二个窗口重复该过程:

var window2 = _kernel.Get<IParentWindow>();
window2.WindowContent = _kernel.Get<IControl2>() ;
_kernel.Get<IWindowManager>().ShowWindow(window2, null, windowSettings);

这将使用WPF中的Caliburn.Micro创建两个窗口,其中包含在外部程序集中定义的用户控件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM