簡體   English   中英

在Monomac / Xamarin C中創建無窗口菜單欄圖標應用程序#

[英]Creating a windowless menu-bar icon application in Monomac/Xamarin C#

我試圖在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;
    }
}

有沒有人知道在MonoMac中創建一個沒有停靠圖標且只有菜單欄圖標的無窗口應用程序的好方法?

謝謝,

BB

嘗試為“主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; } 
  • 在MainMenu.xib中,我刪除了應用程序菜單

  • 在MainMenu.xib中,我添加了一個自定義對象並將其類型設置為AppController
  • 我在.xib中創建了狀態菜單,然后通過插座將其連接到AppController
  • 在info.plist中,我將“Application is agent(UIElement)”值添加為字符串並將其設置為“1”

嘗試使用.xib。 如果它不起作用,也許我可以分享我的項目讓你分開。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM