简体   繁体   English

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

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

I have a need to display two WPF controls at one time, in separate windows, at startup. 我需要在启动时同时在单独的窗口中一次显示两个WPF控件。 The parent windows are of the same type and the user controls (and parent window) are defined in a separate assembly, which is only referenced by the host project. 父窗口具有相同的类型,并且用户控件(和父窗口)在单独的程序集中定义,仅由宿主项目引用。 I am using Caliburn.Micro as an MVVM framework and Ninject for IoC. 我正在使用Caliburn.Micro作为MVVM框架和Ninject for IoC。 How can this be done? 如何才能做到这一点?

All viewmodels are derived from PropertyChangedBase. 所有视图模型均来自PropertyChangedBase。 I have already setup AppBootstrapper to define the Caliburn.Micro standard bindings, such as WindowManager: 我已经设置了AppBootstrapper来定义Caliburn.Micro标准绑定,例如WindowManager:

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

and created an override for OnStartup that created Control1: 并为创建Control1的OnStartup创建了替代:

DisplayRootViewFor<IWindow1>();

The user control is supplied to the parent window in a ContentControl as a window context, like this: 用户控件作为窗口上下文提供给ContentControl中的父窗口,如下所示:

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

Finally, I also provided an override to SelectAssemblies, so that Caliburn.Micro could find the views and viewmodels in the dll: 最后,我还提供了对SelectAssemblies的替代,以便Caliburn.Micro可以在dll中找到视图和视图模型:

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

I have tried several possible solutions, none of which worked: 我尝试了几种可能的解决方案,但都没有成功:

  1. Open the Window2 from the constructor of the Window1 viewmodel (using WindowManager.ShowWindow). 从Window1视图模型的构造函数中打开Window2(使用WindowManager.ShowWindow)。 However, this only opened Window2, and never opened Window1. 但是,这仅打开Window2,而从未打开Window1。 Probably not a good idea anyway.. 可能不是一个好主意。

  2. Create one window in AppBootstrapper.OnStartup, and another window using the App.xaml StartupUri, however this did not allow me to include the user control inside a generic parent window. 在AppBootstrapper.OnStartup中创建一个窗口,并在App.xaml StartupUri中创建另一个窗口,但是这不允许我将用户控件包含在通用父窗口中。 All I could do was open an empty parent window. 我所能做的就是打开一个空的父窗口。

  3. Call DisplayRootViewFor() on the interface for each window to open on startup. 在每个窗口的界面上调用DisplayRootViewFor()以在启动时打开。 The problem with this is there is no way to set the window content, so you don't get the custom parent window, just the default window provided by Caliburn.Micro. 这样做的问题是无法设置窗口内容,因此您无法获得自定义父窗口,而只能获得Caliburn.Micro提供的默认窗口。

Here is how I did it: 这是我的做法:

In AppBootstrapper.cs, instead of calling DisplayRootViewFor, create an instance of the parent window first: 在AppBootstrapper.cs中,而不是调用DisplayRootViewFor,请首先创建父窗口的实例:

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

Provide the user control to the parent window context: 向父窗口上下文提供用户控件:

parentWindow = _kernel.Get<IControl1>();

Open the window with WindowManager.ShowWindow: 使用WindowManager.ShowWindow打开窗口:

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

Simply repeat the process for the second window: 只需对第二个窗口重复该过程:

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

This will create two windows, containing user controls defined in an external assembly, using Caliburn.Micro in WPF. 这将使用WPF中的Caliburn.Micro创建两个窗口,其中包含在外部程序集中定义的用户控件。

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

相关问题 Caliburn.micro,WPF和两个组合框 - Caliburn.micro, WPF and two comboboxes 我需要使用带有Caliburn.Micro的WPF在DataGrid上显示对象字典的数据,每个对象都有自己的字典 - I need to display data from a dictionary of objects each with their own dictionary on a DataGrid using WPF with Caliburn.Micro 绑定两个文本框使用caliburn.micro c#wpf保持同步 - Binding two textbox keep synchronized using caliburn.micro c# wpf 在WPF中使用INotifyDataErrorInfo和嵌入式UserControl(与Caliburn.Micro)一起使用 - Using INotifyDataErrorInfo with embedded UserControl in WPF (with Caliburn.Micro) 使用 Caliburn.Micro 的单实例 WPF 应用程序 - Single instance WPF application using Caliburn.Micro 使用WPF和Caliburn.Micro在视图中添加多个视图 - Add multiple views inside a view using WPF and Caliburn.Micro 如何使用 Caliburn.Micro MVVM 正确设置 WPF 文本框的样式? - How to properly style a WPF TextBox using Caliburn.Micro MVVM? WPF / Caliburn.Micro-使用IDataErrorInfo进行输入验证 - WPF/Caliburn.Micro - Input Validation using IDataErrorInfo 如何使用caliburn.micro WPF抓取webbrowser.document - How to grab webbrowser.document using caliburn.micro WPF 使用Caliburn.Micro MVVM WPF进行视图导航的建议 - Advice on Views navigation using Caliburn.Micro MVVM WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM