选择'关闭所有Windows'如何关闭所有窗口?在应用程序的任务栏图标上?

时间:2016-07-06 17:06:58

标签: wpf taskbar

所以我现在已经研究了这个问题已经有好几个星期了,而且还没有真正想出为什么这种方法不能正常工作的答案......我'我甚至研究过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>

这里是它背后的代码......当我点击一个按钮时会启动一个辅助窗口。它将父窗口(所有者属性)设置为主窗口,就像我所见的所有示例都说应该设置它,然后在其上调用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个窗口,然后全部关闭...有时会逐个关闭几个窗口,然后关闭其余窗口...有时会关闭所有子窗口并只打开主窗口。毋庸置疑,我对这种行为感到非常困惑,因为它似乎没有遵循任何明显的模式......任何帮助都非常感谢!希望这个例子足以解释我想要的内容......

1 个答案:

答案 0 :(得分:0)

那么你可以添加一个事件来关闭事件植入的每个窗口。试试这个例子:

第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();
    }