Monobjc :: Add NSStatusItem
I'm trying to add an NSStatusItem from within the Monobjc bridge for Mono. The function gets called, and doesn't throw an exception, but my icon doesn't appear :(.
Here 's my code:
#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.Ac开发者_StackOverflow中文版tionEvent += 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
}
}
}
Thanks in advance,
Yvan
You have to retain the _statusItem
variable right after its creation:
_statusItem = NSStatusBar.SystemStatusBar.StatusItemWithLength(32f);
_statusItem.Retain();
精彩评论