gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:35:16 26 4
gpt4 key购买 nike

我需要在启动时在单独的窗口中一次显示两个 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;
}

我尝试了几种可能的解决方案,但都没有奏效:
  • 从 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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com