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

所有 View 模型均来自PropertyChangedBase。我已经设置了AppBootstrapper来定义Caliburn.Micro标准绑定(bind),例如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中找到 View 和 View 模型:
protected override IEnumerable<Assembly> SelectAssemblies()
{
    var assemblies = base.SelectAssemblies().ToList();
    assemblies.Add(typeof(IControl1).GetTypeInfo().Assembly);
    return assemblies;
}

我尝试了几种可能的解决方案,但都没有成功:
  • 从Window1 View 模型的构造函数中打开Window2(使用WindowManager.ShowWindow)。但是,这仅打开Window2,而从未打开Window1。可能不是一个好主意..
  • 在AppBootstrapper.OnStartup中创建一个窗口,并使用App.xaml StartupUri创建另一个窗口,但是,这不允许我将用户控件包括在通用父窗口中。我所能做的就是打开一个空的父窗口。
  • 在界面上为每个窗口调用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创建两个窗口,其中包含在外部程序集中定义的用户控件。

    关于c# - 使用Caliburn.Micro同时显示两个WPF窗口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32897291/

    10-11 22:30
    查看更多