我需要在启动时在单独的窗口中一次显示两个 WPF 控件。父窗口的类型相同,并且用户控件(和父窗口)在单独的程序集中定义,该程序集仅由宿主项目引用。我使用 Caliburn.Micro 作为 MVVM 框架和用于 IoC 的 Ninject。如何才能做到这一点?
所有 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;
}
我尝试了几种可能的解决方案,但都没有奏效:
最佳答案
这是我的做法:
在 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/