如何使对象成为窗口的委托?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
带图片: 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 成为主窗口的委托并对主窗口关闭做出反应,请按照以下步骤操作。
双击MainWindow.xib 打开Xcode。
在 Xcode 中,找到主窗口。它是一个看起来像窗口的大东西。
图1:标题为“Window”的大东西是主窗口。
您将看到一个名为“delegate”的出口。
图2:“奥特莱斯”之一是“代表”。
图3:蓝色盒子是一个物体。
图4:蓝色框属于窗口图标下方。
图5:更新蓝色框的类名。
图 6:按住 Control 键拖动时出现的菜单。
我们的 MainWindow 对象现在是主窗口的委托。这意味着它可以对窗口上发生的事情做出反应。
将以下代码添加到MainWindow.cs:
[导出("windowWillClose:")]
公共无效WindowWillClose(NSNotification通知)
{
Console.WriteLine("windowWillClose:");
NSApplication.SharedApplication.Terminate(this);
}
告诉编译器(可能是编译器,但技术上可能有其他实用程序执行此操作)以下方法声明是所声明的 Objective-C 方法的 C# 等效项。该方法可以有不同的实际名称,但名称应该足够相似,以便我们可以轻松识别它。我通常只是将第一个字母更改为大写版本以符合 C# 风格。
如果没有,请重复此处介绍的所有步骤,直到出现为止。
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.
Double-click MainWindow.xib to open Xcode.
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.
You will see one outlet called "delegate".
Picture 2: One of the "Outlets" is "delegate".
Picture 3: The blue box is an object.
Picture 4: The blue box belongs under the window icon.
Picture 5: Update the class name for the blue box.
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.
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.
If not, repeat all the steps told here until it does.
您提到,您将 MainWindowController 作为委托,我建议创建继承自 NSWindowDelegate 的单独/内部类,覆盖 WillClose(NSNotification 通知)< /code> 方法并将其设置为 Window Delegate。工作完成了。
代码:
在 AwakeFromNib 方法中添加以下内容:
You mentioned that, you made the
MainWindowController
as delegate I would suggest that, create separate/inner class that inherits fromNSWindowDelegate
, override theWillClose (NSNotification notification)
method and set it as Window Delegate. Job is done.Code:
In AwakeFromNib method add this: