Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make a tray-icon-only C# application in MonoMac (no dock icon)?
    primarykey
    data
    text
    <p>I am trying to create an application that will have a tray icon only, and not appear in the taskbar. (similar to Dropbox) I need to create both Windows and Mac version of the application, so I tried using MonoMac to create the Mac front-end. </p> <p>What is the best way to create a tray-only application in MonoMac? </p> <p>All the resources I have found say to do one of two things:</p> <ul> <li>Add <code>&lt;key&gt;LSUIElement&lt;/key&gt;&lt;string&gt;1&lt;/string&gt;</code> to the <code>Info.plist</code> file.</li> <li>Add the following code to the <code>FinishedLaunching</code> event in the <code>AppDelegate</code> class: <code>NSApplication.SharedApplication.ActivationPolicy = NSApplicationActivationPolicy.Accessory;</code></li> </ul> <p>I have tried all combinations of these two, but it seems that as soon as I try to instantiate a C# <code>System.Timers.Timer</code>, the icon <em>reappears</em> in the dock at the bottom of the screen. Am I missing something about how OSX handles background applications?</p> <p><strong>What am I doing wrong? Is there a better way to make a background application that has an upper tray icon but no bottom dock icon in OSX?</strong></p> <p>(This is very similar to this <a href="https://stackoverflow.com/questions/4414629/system-tray-only-no-dock-icon-application-using-c-sharp-mono-on-mac">SO question</a>, but that question was from a couple years ago and was never fully answered, so I'm hoping there might be a more complete answer out there.)</p> <hr> <p>Here's the code I have so far:</p> <pre><code>public partial class AppDelegate : NSApplicationDelegate { MyServiceObject currentServiceObject; 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 My Application", (a,b) =&gt; { 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 + @"/notify-icon.icns")); 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; // Start running the program -- If I comment out then no dock icon appears currentServiceObject = new MyServiceObject(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload