如何使对象成为窗口的委托?

发布于 2024-12-08 21:02:29 字数 385 浏览 1 评论 0原文

我正在尝试使用 Xcode 4 来弄清楚 MonoMac,并且大多数事情似乎都可以工作。但是,我不知道如何让 windowWillClose: 工作。

我添加

[Export ("windowWillClose:")]
    void windowWillClose(NSNotification notification)
    {
        Environment.Exit(0);    
    }

到 MainWindow.cs 并使 MainWindow 成为窗口的委托。 (我尝试将 MainWindowController 设为委托,但这根本不起作用。)

但是,当我关闭窗口时,委托方法不会被调用。

我在忽略什么?

I am trying to figure out MonoMac with Xcode 4 and most things appear to work. However, I cannot figure out how to get windowWillClose: to work.

I added

[Export ("windowWillClose:")]
    void windowWillClose(NSNotification notification)
    {
        Environment.Exit(0);    
    }

to MainWindow.cs and made MainWindow a delegate for the window. (I tried making MainWindowController the delegate but that simply didn't work.)

However, when I close the window, the delegate method doesn't get called.

What am I overlooking?

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

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

发布评论

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

评论(2

陪你搞怪i 2024-12-15 21:02:29

带图片: http://www.netneurotic.net/Mono/MonoMac-windowWillClose.html< /a>

这里的技巧是创建一个调用Environment.Exit() 的方法来像任何其他.NET 应用程序一样退出应用程序。

另一个技巧是注意当 Cocoa 对象处于活动状态时,Environment.Exit() 不起作用。但 NSApplication.SharedApplication.Terminate(this) 有效。我不知道如何以这种方式返回错误代码。

Cocoa 以及 MonoMac 使用“委托”来允许一个对象在另一个对象发生某些情况时做出反应。当主窗口关闭时,我们将使用这个概念来调用 Terminate()。

Cocoa 对象有“出口”,它看起来是指向其他对象的指针。我不知道这在技术上是否是正确的描述。没关系。出口之一是“委托”,我们将其设置为包含我们想要在窗口关闭时调用的方法的对象。因此,我们必须将主窗口的委托出口设置为我们的对象。

我们将使用 MainWindow.cs 中定义的 MainWindow 类作为主窗口的委托。我想这意味着我们正在使用一个对象作为它自己的委托或类似的东西。它仍然会起作用。

要使 MainWindow 成为主窗口的委托并对主窗口关闭做出反应,请按照以下步骤操作。

  1. 双击MainWindow.xib 打开Xcode。

  2. 在 Xcode 中,找到主窗口。它是一个看起来像窗口的大东西。

图1:标题为“Window”的大东西是主窗口。

  1. 右键单击窗口的标题栏可显示窗口的插座。

您将看到一个名为“delegate”的出口。

图2:“奥特莱斯”之一是“代表”。

  1. 找到“对象库”和其中的蓝色框。蓝色盒子是一个物体。

图3:蓝色盒子是一个物体。

  1. 将蓝色框拖到窗口左侧带有图标的灰色物体上。

图4:蓝色框属于窗口图标下方。

  1. 使蓝色框成为“MainWindow”对象。单击蓝色框并将其类更改为“MainWindow”。

图5:更新蓝色框的类名。

  1. 按 Ctrl 键并从窗口标题栏拖动到蓝色框中。然后在出现的菜单中选择“委托”选项。

图 6:按住 Control 键拖动时出现的菜单。

我们的 MainWindow 对象现在是主窗口的委托。这意味着它可以对窗口上发生的事情做出反应。

  1. 将以下代码添加到MainWindow.cs:

    [导出("windowWillClose:")]
    公共无效WindowWillClose(NSNotification通知)
    {
    Console.WriteLine("windowWillClose:");
    NSApplication.SharedApplication.Terminate(this);
    }

告诉编译器(可能是编译器,但技术上可能有其他实用程序执行此操作)以下方法声明是所声明的 Objective-C 方法的 C# 等效项。该方法可以有不同的实际名称,但名称应该足够相似,以便我们可以轻松识别它。我通常只是将第一个字母更改为大写版本以符合 C# 风格。

  1. 编译应用程序,修复所有拼写错误,然后运行它。尝试关闭窗口。应用程序将退出。

如果没有,请重复此处介绍的所有步骤,直到出现为止。

With pictures: http://www.netneurotic.net/Mono/MonoMac-windowWillClose.html

The trick here is to create a method that calls Environment.Exit() to quit the application like any other .NET application.

The other trick is to notice that Environment.Exit() does not work when Cocoa objects are alive. But NSApplication.SharedApplication.Terminate(this) works. I don't know how to return an error code that way.

Cocoa, and thus MonoMac, uses "delegates" to allow one object to react when something happens to another object. We will use this concept to call Terminate() when the main window closes.

A Cocoa object has "outlets", which appear to be pointers to other objects. I don't know if this is technically the correct description. It doesn't matter. One of the outlets is the "delegate" which we will set to the object that contains the method that we want called when the window is closed. Hence we have to set the main window's delegate outlet to our object.

We will use the MainWindow class defined in MainWindow.cs as delegate for the main window. I guess that means we are using an object as its own delegate or something like that. It will still work.

To make MainWindow the delegate for the main window and react to the main window closing follow these steps.

  1. Double-click MainWindow.xib to open Xcode.

  2. In Xcode, find the main window. It's the big thing that looks like a window.

Picture 1: The big thing titled "Window" is the main window.

  1. Right-click on the title bar of the window to display the window's outlets.

You will see one outlet called "delegate".

Picture 2: One of the "Outlets" is "delegate".

  1. Find the "Object Library" and the blue box in it. The blue box is an object.

Picture 3: The blue box is an object.

  1. Drag the blue box to the grey thingy with the icons to the left of the window.

Picture 4: The blue box belongs under the window icon.

  1. Make the blue box a "MainWindow" object. Click on the blue box and change its class to "MainWindow".

Picture 5: Update the class name for the blue box.

  1. Press the control key and drag from the window title bar into the blue box. Then select the "delegate" option in the menu that appears.

Picture 6: The menu that appears when you control-drag.

Our MainWindow object is now the delegate for the main window. This means it can react to things that happen to the window.

  1. Add the following code to MainWindow.cs:

    [Export("windowWillClose:")]
    public void WindowWillClose(NSNotification notification)
    {
    Console.WriteLine("windowWillClose:");
    NSApplication.SharedApplication.Terminate(this);
    }

The [Export ("windowWillClose:")] statement tells the compiler (presumably the compiler, but maybe some other utility technically does the being told) that the following method declaration is the C# equivalent of the Objective-C method announced. The method can have a different actual name but should be named similarly enough so that we can identify it easily enough. I usually just change the first letter to its upper-case version to conform to C# style.

  1. Compile the application, fix all the typos you made, then run it. Try closing the window. The application will quit.

If not, repeat all the steps told here until it does.

要走就滚别墨迹 2024-12-15 21:02:29

您提到,您将 MainWindowController 作为委托,我建议创建继承自 NSWindowDelegate 的单独/内部类,覆盖 WillClose(NSNotification 通知)< /code> 方法并将其设置为 Window Delegate。工作完成了。

代码:

    class MyDelegate:NSWindowDelegate
    {
        public override void WillClose (NSNotification notification)
        {

        }
    }

在 AwakeFromNib 方法中添加以下内容:

Window.Delegate = new MyDelegate();

You mentioned that, you made the MainWindowControlleras delegate I would suggest that, create separate/inner class that inherits from NSWindowDelegate, override the WillClose (NSNotification notification) method and set it as Window Delegate. Job is done.

Code:

    class MyDelegate:NSWindowDelegate
    {
        public override void WillClose (NSNotification notification)
        {

        }
    }

In AwakeFromNib method add this:

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