如何让 Show(n)Windows 落后于 shell Window Caliburn.Micro

How to make Show(n)Windows Fall behind shell Window Caliburn.Micro

我有一个ShellViewModel是导体。它有一系列菜单项,可打开选项卡控件中 "hosted" 的各种 view/viewmodels,类似于此处 Caliburn Micro Composition Documentation 中的简单 MDI 示例 某些视图项不应托管在选项卡控件内,而需要是非模态的 windows。我可以使用 WindowManager 的 ShowWindow 方法打开它们。

但是,当这些 windows 失去焦点时,它们仍然是 shell window 的 "on top"。

ShellView.xaml

<Window x:Class="SimpleCaliburnWpf.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SimpleCaliburnWpf"
    mc:Ignorable="d"
    Title="ShellView" Height="300" Width="300">
<Grid>
    <Button Name="OpenWindow">Open A New Window!</Button>
</Grid>

ShellViewModel.cs

public class ShellViewModel : Conductor<IScreen>.Collection.AllActive
{
    private IWindowManager _windowManager;
    public ShellViewModel(IWindowManager windowManager)
    {
        _windowManager = windowManager;
    }

    public void OpenWindow()
    {
        var vm = new FirstViewModel();
        _windowManager.ShowWindow(vm);
    }
}

FirstViewModel.cs

public class FirstViewModel : Screen { }

如果单击 shell,有人可以提供有关这些 windows 如何生活在 shell 后面的建议。

如您所见,ShellView已激活,但FirstView仍在前面。

您可以创建自定义 WindowManager class,将创建的 window 的 Owner 属性 设置为 null

public class CustomWindowManager : WindowManager
{
    protected override Window CreateWindow(object rootModel, bool isDialog, object context, IDictionary<string, object> settings)
    {
        Window window = base.CreateWindow(rootModel, isDialog, context, settings);
        window.Owner = null;
        window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        return window;
    }
}

不要忘记在您的引导程序中注册管理器:

protected override void Configure()
{
    ...
    _container.Singleton<IWindowManager, CustomWindowManager>();
}

I would like to close these windows when the shell closes, but now that it doesn't own them, this is difficult. Any suggestions here?

您可以处理所有者 window 的 Closed 事件:

protected override Window CreateWindow(object rootModel, bool isDialog, object context, IDictionary<string, object> settings)
{
    Window window = base.CreateWindow(rootModel, isDialog, context, settings);
    Window ownerWindow = window.Owner;
    window.Owner = null;
    window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    if (ownerWindow != null)
        ownerWindow.Closed += (s, e) => window.Close();
    return window;
}