处理 MonoMac 退出/窗口关闭事件

发布于 2024-12-10 20:38:28 字数 592 浏览 4 评论 0原文

与我之前发布的问题类似:处理 WPF 退出事件

我发现Objective C 中的解决方案,但我不熟悉如何将其移植到 Mono。

编辑

我发现我可以使用以下覆盖来执行我想要的操作:

NSApplicationTerminateReply ApplicationShouldTerminate (NSApplication sender)

但是,如果我关闭主窗口,现在就会出现问题,因为这实际上是我想要开始调用应用程序退出的地方。我已经有一个返回 true 的 ApplicationShouldTerminateAfterLastWindowClosed 覆盖,因此终止覆盖被正确调用。但是当我返回“取消”时,应用程序正在运行,没有窗口。有没有办法拦截窗口关闭事件?

Similar to this question I posted earlier: Handle a WPF Exit Event

I found a solution in Objective C, but I'm not familiar with how to port this with Mono.

EDIT

I found that I could use the following override to do what I wanted to:

NSApplicationTerminateReply ApplicationShouldTerminate (NSApplication sender)

However, there is a problem now if I close my MainWindow since that is actually where I want to start calling application exit. I already have an override for ApplicationShouldTerminateAfterLastWindowClosed that returns true, so the terminate override is being called correctly. But when I'm returning Cancel, the app is running, sans window. Is there a way to intercept the window closing event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2024-12-17 20:38:28

这就是我最终所做的。我创建了一个名为 MainWindowDelegate 的新类:

public class MainWindowDelegate : MonoMac.AppKit.NSWindowDelegate
{
    public override WindowShouldClose (MonoMac.Foundation.NSObject sender)
    {
        return false;
    }
}

然后,在我的 MainWindowController 类中:

public class MainWindowController
{
    private MainWindowDelegate _delegate;

    // Shared initialization code
    void Initialize()
    {
        _delegate = new MainWindowDelegate();
    }

    public override void WindowDidLoad()
    {
        Window.Delegate = _delegate;
    }
}

This is what I ended up doing. I created a new class called MainWindowDelegate:

public class MainWindowDelegate : MonoMac.AppKit.NSWindowDelegate
{
    public override WindowShouldClose (MonoMac.Foundation.NSObject sender)
    {
        return false;
    }
}

Then, in my MainWindowController class:

public class MainWindowController
{
    private MainWindowDelegate _delegate;

    // Shared initialization code
    void Initialize()
    {
        _delegate = new MainWindowDelegate();
    }

    public override void WindowDidLoad()
    {
        Window.Delegate = _delegate;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文