我正在嘗試創建一個應用程序,它只有一個托盤圖標,並且不會顯示在任務欄中。 (類似於Dropbox)我需要創建Windows和Mac版本的應用程序,所以我嘗試使用MonoMac創建Mac前端。如何在MonoMac中製作僅托盤圖標的C#應用程序(沒有停靠圖標)?
在MonoMac中創建托盤專用應用程序的最佳方法是什麼?
我發現所有資源,要說到做到的兩兩件事之一:
- 添加
<key>LSUIElement</key><string>1</string>
到Info.plist
文件。 - 下面的代碼添加到
FinishedLaunching
事件在AppDelegate
類:NSApplication.SharedApplication.ActivationPolicy = NSApplicationActivationPolicy.Accessory;
我已經試過這兩個的所有組合,但似乎只要我嘗試實例化一個C#System.Timers.Timer
,圖標重新出現在屏幕底部的碼頭中的。我是否錯過了OSX如何處理後臺應用程序?
我在做什麼錯?有沒有更好的方法來製作OSX中有上部托盤圖標但底部停靠圖標的後臺應用程序?
(這是非常相似,這SO question,但幾年前這個問題是從一對夫婦,並沒有完全回答,所以我希望有可能是一個更完整的答案在那裏。)
這裏是我到目前爲止的代碼:
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) => { 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();
}
}
愛知道答案也是這樣! – berg 2012-07-24 20:21:04