我正在试着制作一个非常小的应用程序,它只位于状态栏中,响应键盘快捷键,并具有上下文菜单.每当通过选项 Select 或用户单击离switch 闭此菜单时,都会调用应用程序OnExit方法.
我在.NET7上创建了一个新的WPF应用程序,删除了MainWindow.xaml
,并在App.xaml
中删除了对它的引用,这是一个最小版本,然后是App.xaml.cs
中的整个代码:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
namespace CLP_MVP;
public partial class App : System.Windows.Application
{
private readonly NotifyIcon _taskbarIcon = new();
private readonly ContextMenu _contextMenu = new();
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
_taskbarIcon.Icon = ResourceIcons.clp;
_taskbarIcon.Visible = true;
_contextMenu.Items.Add(new MenuItem() { Header = "Test" });
_taskbarIcon.Click += OpenMenu;
}
private void OpenMenu(object? sender, EventArgs e)
=> _contextMenu.IsOpen = true;
protected override void OnExit(ExitEventArgs e)
=> base.OnExit(e);
}
我在点击离开时得到了不一致的结果(我希望在这里有任何见解),但我已经添加了一个菜单项,该菜单项带有一个未处理的点击事件,有效地关闭了菜单.
通过该框架的调用堆栈是:
> CLP_MVP.dll!CLP_MVP.App.OnExit(System.Windows.ExitEventArgs e) Line 25 C#
PresentationFramework.dll!System.Windows.Application.DoShutdown() Line 1582 C#
PresentationFramework.dll!System.Windows.Application.ShutdownImpl() Line 2210 C#
PresentationFramework.dll!System.Windows.Application.ShutdownCallback(object arg) Line 2198 C#
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Line 103 C#
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Line 36 C#
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl() Line 540 C#
WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(object obj) Line 215 C#
System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 183 C#
WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 172 C#
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke() Line 412 C#
WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue() Line 2095 C#
WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled) Line 2304 C#
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled) Line 295 C#
WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Line 429 C#
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Line 103 C#
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Line 36 C#
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Line 1339 C#
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(nint hwnd, int msg, nint wParam, nint lParam) Line 341 C#
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) Line 2138 C#
PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore) Line 2407 C#
PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window) Line 1695 C#
CLP_MVP.dll!CLP_MVP.App.Main() Line 53 C#
任何关于这方面的帮助或 idea 都将不胜感激.我认为这与应用程序生命周期提前终止有关.由于没有"可见"元素(除了来自不同框架的状态栏图标),应用程序处理程序只是终止执行以避免僵尸进程.如果这一假设是正确的,我将感谢任何关于如何避免这种情况的 idea .
我试着用Hardcodet.Wpf.TaskbarNotification
,结果完全一样.