Monobjc ::添加NSStatusItem

时间:2010-01-30 09:18:02

标签: c# mono nsstatusitem monobjc

我正在尝试从Monobjc桥中为Mono添加一个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
  }
    }
}

提前致谢,

伊凡

1 个答案:

答案 0 :(得分:2)

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

_statusItem = NSStatusBar.SystemStatusBar.StatusItemWithLength(32f);
_statusItem.Retain();