当您在应用程序的任务栏图标上 select 'Close All Windows' 时,如何关闭所有 windows?

How to get all windows close when you select 'Close All Windows' on the app's taskbar icon?

所以我已经研究了好几个星期了,但还没有真正想出为什么它不能正常工作的答案......我什至研究了 JumpLists 看看如果这就是我要找的东西,但也无济于事。此问题涉及当您尝试通过右键单击任务栏上的应用程序图标来 select 'Close All Windows'...

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

contextmenutoolbar 我正在 select 选择 'Close all windows',以供参考(底部的那个,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。它还将 ShutdownMode 设置为仅当主要 window 关闭时...如果主要 window 关闭并且一些次要 [=49],我不希望应用程序仍然 运行 =] 保持打开状态。

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>

这是它背后的代码...当我单击一个按钮时它会启动一个辅助 window。它正在将 parent window(所有者 属性)设置为主要 window,就像我见过的所有示例都说应该设置它一样,然后调用 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();
        }
    }
}

这是 child window、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();
        }
    }
}

如您所见,这些 类 并没有做太多......这是我能写的最简单的代码示例,它显示了我遇到的问题。所以问题是,如果我从任务栏上下文菜单 select 关闭所有 windows,它永远不会关闭所有 windows。相反,它会关闭 child window,并且仍然让主 window 打开。有趣的是,当我这样做时,我听到 windows 对话提示音,几乎就像它被什么东西打断了,但我不知道是什么。

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

Well you could add an event that will close every window the event is implanted in. try this example:

step 1: add a class to your project call it whatever you want, I called it CloseWindowListener, add this code to your class:

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

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

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

step 2: Add the event handler to the window you desire to close when it is called.

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

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

step 3: simply call the event from your main window or where ever you want.

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