C# windows 7中未触发WPF关闭事件?

C# windows 7中未触发WPF关闭事件?,c#,wpf,C#,Wpf,我有一个C#窗口,刚刚发现Windows7下任务栏上的右键单击关闭事件没有被触发 但是,在我正在开发的windows 10 PC上,此操作已得到正确处理。 我的xaml文件如下所示: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quo

我有一个C#窗口,刚刚发现Windows7下任务栏上的右键单击关闭事件没有被触发

但是,在我正在开发的windows 10 PC上,此操作已得到正确处理。 我的xaml文件如下所示:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WCSevenX"
    x:Class="WCSevenX.MainWindow"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"                
    Loaded="Window_Loaded" 
    Closing="OnClosing"  
    Background="#B0CBCBE5" 
    Name="MainWnd" 
    Unloaded="MainWnd_Unloaded" 
    SizeChanged="MainWnd_SizeChanged" 
    PreviewGotKeyboardFocus="MainWnd_PreviewGotKeyboardFocus" 
    PreviewLostKeyboardFocus="MainWnd_PreviewLostKeyboardFocus" 
    GotFocus="MainWnd_GotFocus" 
    GotKeyboardFocus="MainWnd_GotKeyboardFocus">
    <!--body...-->
</Window>
函数
OnClosing
在windows 10下调用,但在windows 7中不调用。在windows 7中,右键单击并关闭无法关闭我的窗口

更新:

今天,我还尝试在WndProc()中捕获
WM_CLOSE
消息

在windows 10中,任务栏上的[X]按钮和右键单击关闭都会收到
WM_close
消息。 在windows 7中,只有[X]按钮接收
WM\u CLOSE
消息


我哪里做错了?

尝试从代码而不是从xaml附加事件:

 this.Closing += OnClosing;
对我来说,这适用于Windows7和Windows10


希望这对您有用。

在应用程序类中管理退出事件

protected override void OnExit(ExitEventArgs e)
    {
        base.OnExit(e);

        this.DispatcherUnhandledException -= new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
    }

我发现我有一个具有dispatcher的线程:

private void StartDlg()
{
    wnd = new MyDlg();
    wnd.Show();
    Dispatcher.Run();  //<-this start an event loop
}
Thread myThread = new Thread(StartDlg);
我的线程将阻止窗口消息循环


在windows 8+中可以,但在windows 7中,这将阻止右键单击关闭消息传递到我的应用程序。添加
InvokeShutdown
将在windows 7中修复。

我在代码中添加了关闭事件,但在windows 7中仍然没有从任务栏右键单击激发。尝试使用Closed event查看它是否被激发,或者是同一个caseClosed event是否动作相同。我使用的是
Window
类,真的需要一个
应用程序
类吗?它不需要花费任何费用,并且允许您管理不通过alt+F4等主窗口的事件。。。。看看这个好例子:-)奇怪,Application.OnExit事件没有引发,在Windows7中从任务栏右键单击。只有X按钮起作用。它在两个项目中对我起作用……我试过编译所有CBR代码并进行测试吗
private void StartDlg()
{
    wnd = new MyDlg();
    wnd.Show();
    Dispatcher.Run();  //<-this start an event loop
}
Thread myThread = new Thread(StartDlg);
Dispatcher.FromThread(myThread).InvokeShutdown();