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

时间:2018-02-07 15:52:08

标签: c# wpf caliburn.micro

我有ShellViewModel指挥。它有一系列菜单项,可以打开各种视图/视图模型,这些视图/视图模型是“托管的”#34;在选项卡控件中,类似于此处Caliburn Micro Composition Documentation中的简单MDI示例 某些视图项不应位于选项卡控件内,而是需要是非模态窗口。我可以使用WindowManager的ShowWindow方法打开它们。

然而,当这些窗户失去焦点时,它们仍然会在顶部" shell窗口。

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,有人可以提供有关这些窗口如何在shell后面生效的建议。

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

1 个答案:

答案 0 :(得分:2)

您可以创建一个自定义WindowManager类,将创建窗口的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>();
}
  

我希望在shell关闭时关闭这些窗口,但是现在它并不拥有它们,这很难。这里有什么建议吗?

您可以处理所有者窗口的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;
}