MonoMac中的子类NSApplication

Dyn*_*lon 2 c# mono cocoa nsapplication monomac

我需要在MonoMac中继承NSApplication以覆盖NSApplication中的sendEvent(C#术语中的SendEvent)方法,以便接收媒体键事件(Play,Pause,Next,Prev on Macbooks),如下所述:聆听mac键盘播放/暂停事件在这里:https://bitbucket.org/nkreeger/whitedragon/src/6f530c8a34a7/component/sbAppleMediaKeyController.mm

到目前为止,我想出了这段代码:

class MainClass
{
    static void Main (string[] args)
    {
        MyApp.Init ();
        MyApp.Main (args);
    }
}
class MyApp : NSApplication
{
    public override void SendEvent (NSEvent theEvent)
    {
        Console.WriteLine ("event received!");
        base.SendEvent (theEvent);
    }
    public override void SetMainMenu (NSMenu aMenu)
    {
        Console.WriteLine ("main menu set!");
        base.SetMainMenu (aMenu);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,由于我没有获得任何控制台输出,因此调用SendEvent或SetMainMenu(我为测试目的而添加的SetMainMenu应该在启动时调用一次).

这样做的正确方法是什么?

Jul*_*ien 6

只是子类化NSApplication不足以让您的子类替换正常的子类.您还必须NSPrincipalClass在应用程序plist中设置.

看到:

https://developer.apple.com/library/mac/#documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html