2

我正在尝试从 Mono 的 Monobjc 桥中添加一个 NSStatusItem 。该函数被调用,并且不会引发异常,但我的图标没有出现:(。

这是我的代码:

#define DEBUG_APP
using System;
using Monobjc.Cocoa;
using Monobjc;
namespace YvanSoftware.TwitMenu
{
  [ObjectiveCClass]
 public class TwitMenuController : NSObject
    {

  [ObjectiveCField]
  NSStatusItem _statusItem;

  [ObjectiveCMessage("awakeFromNib")]
  public void AwakeFromNib() 
  {
   try {
    if (_statusItem == null) {
     NSMenu menu = new NSMenu("");
     _statusItem = NSStatusBar.SystemStatusBar.StatusItemWithLength(32f);
     _statusItem.Image = NSImage.ImageNamed("twitter.png");
     NSMenuItem menuItem = new NSMenuItem("Send tweet!",new IntPtr(),"");
     menuItem.ActionEvent += new ActionEventHandler(showTweetWindow);
     menu.AddItem(menuItem);
     _statusItem.Menu = menu;
     DebugPrint("Status item added??");
    }
   } catch (Exception ex) {
    DebugPrint(ex.Message); 
   }
  }

  public TwitMenuController(IntPtr i)  : base(i) {}

  public TwitMenuController() {} 

  public void showTweetWindow(Id sender) {
   Console.WriteLine("Not implemented. (0x01)");
  }

  [ObjectiveCMessage("showAbout:")]
  public void showAbout() {
   Console.WriteLine("Not implemented (0x02)"); 
  }

  void DebugPrint(string s) {
#if DEBUG_APP
   Console.WriteLine("*** DEBUG: " + s + " ***");
#endif
  }
    }
}

提前致谢,

伊万

4

1 回答 1

2

您必须_statusItem在创建变量后立即保留该变量:

_statusItem = NSStatusBar.SystemStatusBar.StatusItemWithLength(32f);
_statusItem.Retain();
于 2010-02-08T09:34:26.630 回答