NSStatusBarButton.Activate 事件未触发(Xamarin.Mac - C#)
NSStatusBarButton.Activate event does not fire (Xamarin.Mac - C#)
我正在尝试订阅 AppDelegate
的 DidFinishLaunching()
中 NSStatusBarButton
对象的 Activate
事件,但该事件从未被调用。
目的是在单击应用程序的顶部菜单栏图标时收到通知,以便动态填充其内容。
using AppKit;
using Foundation;
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
private NSStatusItem _statusBar;
public override void DidFinishLaunching(NSNotification notification)
{
this._statusBar = NSStatusBar.SystemStatusBar.CreateStatusItem(NSStatusItemLength.Variable);
this._statusBar.Title = "MyApp";
this._statusBar.HighlightMode = true;
this._statusBar.Menu = new NSMenu("MyApp");
// Working example on NSMenuItem object
var someItem = new NSMenuItem("Some Item");
someItem.Activated += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("This one does fire.");
};
this._statusBar.Menu.AddItem(someItem);
// Problem
this._statusBar.Button.Activated += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("This one does not fire.");
};
}
}
它没有触发,因为你附加了一个菜单。按钮动作弹出菜单,按钮的激活事件永远不会被触发。如果删除菜单,按钮事件将 运行.
要么删除菜单并将其用作按钮。然后你的事件就会发生。或者只使用菜单。
如果您想在显示菜单时运行自定义代码,请设置 NSMenu 的委托:
using AppKit;
using Foundation;
public class MyMenuDelegate : NSObject, INSMenuDelegate
{
public void MenuWillHighlightItem(NSMenu menu, NSMenuItem item)
{
}
[Export("menuWillOpen:")]
public void MenuWillOpen(NSMenu menu)
{
// your code here
}
}
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
private NSStatusItem _statusBar;
MyMenuDelegate _menuDel;
public override void DidFinishLaunching(NSNotification notification)
{
_statusBar = NSStatusBar.SystemStatusBar.CreateStatusItem(NSStatusItemLength.Variable);
_statusBar.Title = "MyApp";
_statusBar.HighlightMode = true;
_statusBar.Menu = new NSMenu("MyApp");
_menuDel = new MyMenuDelegate();
_statusBar.Menu.Delegate = _menuDel;
// Working example on NSMenuItem object
var someItem = new NSMenuItem("Some Item");
someItem.Activated += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("This one does fire.");
};
_statusBar.Menu.AddItem(someItem);
}
}
我正在尝试订阅 AppDelegate
的 DidFinishLaunching()
中 NSStatusBarButton
对象的 Activate
事件,但该事件从未被调用。
目的是在单击应用程序的顶部菜单栏图标时收到通知,以便动态填充其内容。
using AppKit;
using Foundation;
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
private NSStatusItem _statusBar;
public override void DidFinishLaunching(NSNotification notification)
{
this._statusBar = NSStatusBar.SystemStatusBar.CreateStatusItem(NSStatusItemLength.Variable);
this._statusBar.Title = "MyApp";
this._statusBar.HighlightMode = true;
this._statusBar.Menu = new NSMenu("MyApp");
// Working example on NSMenuItem object
var someItem = new NSMenuItem("Some Item");
someItem.Activated += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("This one does fire.");
};
this._statusBar.Menu.AddItem(someItem);
// Problem
this._statusBar.Button.Activated += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("This one does not fire.");
};
}
}
它没有触发,因为你附加了一个菜单。按钮动作弹出菜单,按钮的激活事件永远不会被触发。如果删除菜单,按钮事件将 运行.
要么删除菜单并将其用作按钮。然后你的事件就会发生。或者只使用菜单。
如果您想在显示菜单时运行自定义代码,请设置 NSMenu 的委托:
using AppKit;
using Foundation;
public class MyMenuDelegate : NSObject, INSMenuDelegate
{
public void MenuWillHighlightItem(NSMenu menu, NSMenuItem item)
{
}
[Export("menuWillOpen:")]
public void MenuWillOpen(NSMenu menu)
{
// your code here
}
}
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
private NSStatusItem _statusBar;
MyMenuDelegate _menuDel;
public override void DidFinishLaunching(NSNotification notification)
{
_statusBar = NSStatusBar.SystemStatusBar.CreateStatusItem(NSStatusItemLength.Variable);
_statusBar.Title = "MyApp";
_statusBar.HighlightMode = true;
_statusBar.Menu = new NSMenu("MyApp");
_menuDel = new MyMenuDelegate();
_statusBar.Menu.Delegate = _menuDel;
// Working example on NSMenuItem object
var someItem = new NSMenuItem("Some Item");
someItem.Activated += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("This one does fire.");
};
_statusBar.Menu.AddItem(someItem);
}
}