我正在试着制作一个非常小的应用程序,它只位于状态栏中,响应键盘快捷键,并具有上下文菜单.每当通过选项 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,结果完全一样.

推荐答案

默认情况下,当所有窗口关闭时,WPF应用程序将调用隐式关闭.try 在App.Xaml文件中设置ShutdownMode="OnExplicitShutdown".

https://learn.microsoft.com/en-us/dotnet/api/system.windows.application.shutdownmode

请务必添加手动关闭的方法.

Csharp相关问答推荐

为什么.Equals(SS,StringComparison. ClientCultureIgnoreCase)在Net 4.8和6.0之间不同?

如何使用C#和Graph API从Azure Directory获取用户详细信息

C#自定义字典与JSON(de—)serialize

在C#中使用类中的对象值

如何在C#中使用正则表达式抓取用逗号分隔的两个单词?

C#和ASP.NET核心标识:提高GetUserAsync调用的性能

如何通过属性初始化器强制初始化继承记录内的属性?

如何捕获对ASP.NET核心应用程序的所有请求并将其发送到一个页面

在.NET 7.0 API控制器项目中通过继承和显式调用基类使用依赖项注入

用于管理System.Text.Json中的多态反序列化的自定义TypeInfoResolver

Savagger使用Fastendpoint更改用户界面参数

CA1508:';NULL=>;TRUE;始终为';TRUE';.移除或重构条件(S)以避免死代码

自定义列表按字符串的部分排序

Google OAuth令牌交换在.Net中不起作用

使用C#和.NET 7.0无法访问Cookie中的数据

如何让游戏对象在切换场景时被销毁,但在开始新游戏时重新锁定

如何在绑定到数据库的datagridview中向上或向下移动行

除非首先访问使用的终结点,否则本地API上的终结点不起作用

从列表中跳过和获取条目的优雅方式

PowerShell:如何[引用]数组中的元素?