You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI macOS平台关闭时最小化至程序坞方案问询

在.NET MAUI macOS平台实现关闭窗口时最小化至程序坞并后台运行

要实现macOS下关闭窗口时隐藏应用到程序坞、保持后台运行的效果,需要借助macOS原生AppKit API进行平台特定开发,以下是具体实现步骤:

1. 创建自定义窗口处理器(Platforms/MacCatalyst)

在你的MAUI项目的Platforms/MacCatalyst目录下新建CustomWindowHandler.cs,继承自默认的WindowHandler,拦截窗口关闭事件并替换为隐藏逻辑:

using AppKit;
using Microsoft.Maui.Handlers;

namespace YourAppNamespace.Platforms.MacCatalyst;

public class CustomWindowHandler : WindowHandler
{
    protected override void ConnectHandler(NSPortalView platformView)
    {
        base.ConnectHandler(platformView);
        
        // 获取当前窗口的NSWindow实例
        if (platformView.Window is NSWindow window)
        {
            // 拦截窗口关闭事件
            window.Delegate = new CustomWindowDelegate(window);
        }
    }
}

// 自定义窗口委托类
public class CustomWindowDelegate : NSWindowDelegate
{
    private readonly NSWindow _window;

    public CustomWindowDelegate(NSWindow window)
    {
        _window = window;
    }

    // 重写窗口关闭事件
    public override bool WindowShouldClose(NSObject sender)
    {
        // 隐藏窗口而非关闭
        _window.OrderOut(this);
        // 确保应用保持在后台运行
        NSApplication.SharedApplication.SetActivationPolicy(NSApplicationActivationPolicy.Regular);
        
        // 返回false取消默认关闭操作
        return false;
    }

    // 处理程序坞图标点击事件,恢复窗口显示
    public override void ApplicationShouldHandleReopen(NSApplication sender, bool hasVisibleWindows)
    {
        if (!hasVisibleWindows)
        {
            _window.MakeKeyAndOrderFront(this);
        }
    }
}

2. 在MauiProgram中注册自定义处理器

修改项目根目录的MauiProgram.cs,将默认的WindowHandler替换为我们的自定义处理器:

using Microsoft.Maui.LifecycleEvents;
using YourAppNamespace.Platforms.MacCatalyst;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            });

        // 注册macOS自定义窗口处理器
#if MACCATALYST
        builder.ConfigureMauiHandlers(handlers =>
        {
            handlers.AddHandler<IWindow, CustomWindowHandler>();
        });
#endif

        return builder.Build();
    }
}

3. 验证效果

运行macOS版本的应用:

  • 点击窗口关闭按钮,窗口会隐藏,程序坞图标保持存在,应用在后台运行
  • 点击程序坞图标,窗口会重新显示
  • 若要彻底退出应用,可右键程序坞图标选择"退出",或使用Cmd+Q快捷键

关于你之前尝试方案的说明

  • LifecycleEvents未生效:MAUI的跨平台生命周期事件主要针对应用级状态,无法直接拦截窗口级的关闭操作,需要借助平台原生API处理
  • deactivated事件无法隐藏窗口:deactivated是窗口失去焦点的事件,而非关闭事件,时机不对;且该事件无法阻止窗口关闭的默认行为,必须通过WindowShouldClose委托来拦截

内容的提问来源于stack exchange,提问作者Archie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 19:02:42