Frage

I am attempting to create an application in MonoMac/Xamarin.Mac that does not have a dock icon, nor a visible window when it launches and only an icon in the top-right menu bar.

I have set LSUIElement = 1 (tried both String and Boolean types) in Info.plist, but the status menu icon isn't displayed at all when the application launches. The only way I can get it to appear is by removing the LSUIElement flag, although then the dock icon becomes visible.

The snippet I am using is:

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;
    }
}

Does anyone know of a good way of creating a windowless application in MonoMac that doesn't have a dock icon and only a menu bar icon?

Thanks,

BB

War es hilfreich?

Lösung

Try specifying a .xib for the "Main nib file name'

I did this some time ago, and it works fine for me. Something like this:

  • New monomac project
  • Created an 'AppController' class in C#:

    [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;
        }
    
  • In MainMenu.xib, I deleted the application menu

  • In MainMenu.xib, I added an custom object and set it's type to AppController
  • I created my status menu in the .xib and then connected it to the AppController with an outlet
  • In info.plist, I add the "Application is agent (UIElement)" value as a string and set it to "1"

Try it out with a .xib. If it doesn't work, maybe I can share my project for you to take apart.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top