【问题标题】:How to get all windows close when you select 'Close All Windows' on the app's taskbar icon?当您在应用程序的任务栏图标上选择“关闭所有窗口”时,如何关闭所有窗口?
【发布时间】:2016-07-06 17:06:58
【问题描述】:

所以我已经对此进行了好几个星期的研究,但还没有真正想出为什么它不能正常工作的答案......我什至研究了 JumpLists 看看这是否是我一直在寻找的,但也无济于事。当您尝试通过右键单击任务栏上的应用程序图标来选择“关闭所有 Windows”时,此问题与...

例如,这是我编写的一个非常小而简单的 WPF 应用程序,用于演示我遇到的问题。这是任务栏中的应用程序图标及其上下文菜单中的选择...

contextmenutoolbar 我正在选择“关闭所有窗口”选项,以供参考(底部的窗口,左侧有 X)。

这是一个 WPF 应用程序,这里是 App.xaml 的代码:

<Application x:Class="CloseAllWindows.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         >
<Application.Resources>

</Application.Resources>
</Application>

这里是 App.xaml.cs,它启动 MainWindow。它还将应用程序的 MainWindow 属性设置为实例化的 MainWindow。它还将 ShutdownMode 设置为仅在主窗口关闭时...如果主窗口关闭并且某些辅助窗口保持打开状态,我不希望应用程序仍然运行。

using System.Windows;

namespace CloseAllWindows
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        { 
            ShutdownMode = ShutdownMode.OnMainWindowClose;
            var mainWindow = new MainWindow();
            Application.Current.MainWindow = mainWindow;
            mainWindow.Show();
        }
    }
}

这是 MainWindow.xaml 的代码:

<Window x:Class="CloseAllWindows.MainWindow"
        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:CloseAllWindows"
        mc:Ignorable="d"

        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button Content="NewWindow" Click="ButtonBase_OnClick"></Button>
    </StackPanel>
</Window>

这是它背后的代码...当我单击一个按钮时它会启动一个辅助窗口。它将父窗口(Owner 属性)设置为主窗口,就像我看到的所有示例都说应该设置它,然后在其上调用 Show()。

using System;
using System.ComponentModel;
using System.Windows;

namespace CloseAllWindows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var childWindow = new ChildWindow {Owner = this};
            childWindow.Show();
        }
    }
}

这是子窗口的代码,ChildWindow.xaml:

<Window x:Class="CloseAllWindows.ChildWindow"
        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:CloseAllWindows"
        mc:Ignorable="d"
        Title="ChildWindow" Height="300" Width="300">
    <Grid>

    </Grid>
</Window>

还有后面对应的代码,ChildWindow.xaml.cs:

using System;
using System.Windows;

namespace CloseAllWindows
{
    /// <summary>
    /// Interaction logic for ChildWindow.xaml
    /// </summary>
    public partial class ChildWindow : Window
    {
        public ChildWindow()
        {
            InitializeComponent();
        }
    }
}

如您所见,这些类的功能并不多……这是我可以编写的最简单的代码示例,它显示了我遇到的问题。所以问题是,如果我从任务栏上下文菜单中选择关闭所有窗口,它永远不会关闭所有窗口。相反,它会关闭一个子窗口,但仍使主窗口保持打开状态。有趣的是,当我执行此操作时,我会听到 windows 对话提示音,几乎就像它被什么东西打断了一样,但我不知道是什么。

它的行为似乎也很随机...如果我生成 20 个窗口,它有时会关闭 6 个窗口,然后全部关闭...有时它会一个接一个地关闭几个窗口,然后关闭其余的...有时它会关闭所有子窗口,只打开主窗口。不用说,我对这种行为感到非常困惑,因为它似乎没有遵循任何明显的模式......非常感谢任何帮助!希望这个例子足以解释我想要理解的内容......

【问题讨论】:

  • 我现在也刚刚注意到,如果我有 4 个子窗口的倍数,单击“关闭所有窗口”将关闭所有窗口,包括主窗口......非常奇怪的行为。不知道这是不是巧合……哈哈
  • 我将考虑这个关闭...我相信没有其他人会尝试回答它...我发现它在 WinForms 中有效,这很奇怪..我将发布另一个 Stack Overflow显示我所做的 WinForms 代码和我所做的 WPF 代码的问题,并询问 WPF 缺少什么(如果有的话)......

标签: wpf taskbar


【解决方案1】:

您可以添加一个事件,该事件将关闭植入该事件的每个窗口。试试这个例子:

第1步:在你的项目中添加一个类,随便你叫什么,我叫它CloseWindowListener,把这段代码添加到你的类中:

public static class CloseWindowListener
{
    public static event EventHandler<EventArgs> ClosingWindows;

    public static void CloseWindows()
    {
        var CWindows = ClosingWindows;

        if (CWindows != null)
        {
            CWindows(null, EventArgs.Empty);
        }
    }
}

第 2 步:将事件处理程序添加到您希望在调用时关闭的窗口。

public partial class TestWindow1 : Window
{
    public TestWindow1 ()
    {
        InitializeComponent();
        CloseWindowListener.ClosingWindows += CloseWindowListener_ClosingWindows;
    }

    private void CloseWindowListener_ClosingWindows(object sender, EventArgs e)
    {
        this.Close();
    }
}

第 3 步:只需从主窗口或任何您想要的位置调用事件。

    private void button_Click_1(object sender, RoutedEventArgs e)
    {
        CloseWindowListener.CloseWindows();
    }

【讨论】:

  • 不确定这是否适用于我的特定应用程序...我不想添加用户按下以关闭所有窗口的新 UI 元素(他们已经可以通过关闭主窗口)。我了解您的代码正在实现什么,但它是通过一个新的 UI 项而不是任务栏“关闭所有 Windows”直接调用它来调用的,除非我遗漏了什么,否则我正在尝试捕获“关闭所有 Windows”事件,如果有的话。当我收到子窗口关闭的事件时,我也无法关闭主窗口。这在我的应用程序中是不正确的行为。谢谢!
  • 这只会关闭你添加事件处理程序的窗口......只是不要将处理程序添加到你的主窗口,它不会被关闭......
  • 问题是,当用户从任务栏中一次性关闭所有窗口时,我希望主窗口与所有子窗口一起关闭。那有意义吗?如果您查看我附在问题中的屏幕截图,也许这可能有助于解释我正在处理的事件是什么?问题是,结果是零星的,我在回答我自己的问题时提到,它只会在我打开正好是 4 个子窗口的倍数时关闭所有窗口......我今天玩了一些后才注意到更多。
猜你喜欢
  • 2021-04-08
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 2019-08-12
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多