在Monomac/Xamarin C中创建无窗口菜单栏图标应用程序#

BB1*_*BB1 3 c# cocoa monomac xamarin.mac

我试图在MonoMac/Xamarin.Mac中创建一个没有停靠图标的应用程序,在启动时也没有可见窗口,并且在右上方的菜单栏中只有一个图标.

我在Info.plist中设置了LSUIElement = 1(尝试了String和Boolean类型),但在应用程序启动时根本不显示状态菜单图标.我可以让它出现的唯一方法是删除LSUIElement标志,尽管Dock图标变得可见.

我使用的代码片段是:

public partial class AppDelegate : NSApplicationDelegate
{
    public AppDelegate ()
    {
    }

    public override void FinishedLaunching (NSObject notification)
    {
        // Construct menu that will be displayed when tray icon is clicked
        var notifyMenu = new NSMenu();
        var exitMenuItem = new NSMenuItem("Quit", 
                                          (a,b) => { System.Environment.Exit(0); }); // Just add 'Quit' command
        notifyMenu.AddItem(exitMenuItem);

        // Display tray icon in upper-right-hand corner of the screen
        var sItem = NSStatusBar.SystemStatusBar.CreateStatusItem(30);
        sItem.Menu = notifyMenu;
        sItem.Image = NSImage.FromStream(System.IO.File.OpenRead(
            NSBundle.MainBundle.ResourcePath + @"/menu_connected.png"));
        sItem.HighlightMode = true;

        // Remove the system tray icon from upper-right hand corner of the screen
        // (works without adjusting the LSUIElement setting in Info.plist)
        NSApplication.SharedApplication.ActivationPolicy = NSApplicationActivationPolicy.Accessory;
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有人知道在MonoMac中创建一个没有停靠图标且只有菜单栏图标的无窗口应用程序的好方法?

谢谢,

BB

The*_*man 6

尝试为"主nib文件名"指定.xib

我之前做过这个,它对我来说很好.像这样的东西:

  • 新的monomac项目
  • 在C#中创建了一个'AppController'类:

    [Register("AppController")]
    public partial class AppController : NSObject
    {
        public AppController()
        {
    
        }
    
        public override void AwakeFromNib()
        {
            var statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem(30);
            statusItem.Menu = statusMenu;
            statusItem.Image = NSImage.ImageNamed("f3bfd_Untitled-thumb");
            statusItem.HighlightMode = true;
        }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在MainMenu.xib中,我删除了应用程序菜单

  • 在MainMenu.xib中,我添加了一个自定义对象并将其类型设置为 AppController
  • 我在.xib中创建了状态菜单,然后将其连接到AppController带有插座的地方
  • 在info.plist中,我将"Application is agent(UIElement)"值添加为字符串并将其设置为"1"

尝试使用.xib.如果它不起作用,也许我可以分享我的项目让你分开.

  • 我创建了一个包含所有文件内容的公开要点.https://gist.github.com/grexican/11252656此外,这里的文件为.zip,我将保留在我的公共分享中,因此它不会像其他下载链接那样过时:https:// letscrate.com/f/toadsoftware/public/MacStatusMenuExample.zip (2认同)