【发布时间】: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 缺少什么(如果有的话)......