简体   繁体   English

托盘图标不显示

[英]Tray Icon not showing up

I have added a tray icon to my program that should show the up and have buttons for toggling certain functionality.我已经在我的程序中添加了一个托盘图标,该图标应该会显示并具有用于切换某些功能的按钮。 However, the tray icon is not showing up.但是,托盘图标未显示。

I have checked that System.Windows.Forms is included, that the Application.Run() method is called after creating the tray icon, that the Visible property of the NotifyIcon object is set to true, that the Icon property is set correctly (tried several different ones, SystemIcons, my application icon, a specified file), and that the Text property is set properly.我检查过是否包含System.Windows.Forms ,在创建托盘图标后调用了Application.Run()方法, NotifyIcon对象的Visible属性是否设置为 true,Icon 属性是否设置正确(已尝试几个不同的,SystemIcons,我的应用程序图标,一个指定的文件),并且 Text 属性设置正确。

I have looked at various SO questions & answers to no avail, it's a Windows Application targeting .NET Framework 4.7.2 , it does not utilize a form if that matters.我查看了各种 SO 问题和答案都无济于事,它是一个针对.NET Framework 4.7.2的 Windows 应用程序,如果重要的话,它不使用表单。

using System;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SomethingSomething
{
    internal static class Program
    {
        static NotifyIcon trayIcon;
        [STAThread]
        static void Main()
        {
            // Start the webserver
            StartWebServer().Wait();

            // Create the tray icon
            CreateTrayIcon();

            // Needed for tray icon
            Application.Run();
        }

        static void CreateTrayIcon()
        {
            trayIcon = new NotifyIcon
            {
                Visible = true,
                Icon = SystemIcons.Information,
                Text = "Current Song" + currentSong
            };

            var menu = new ContextMenu();
            var toggleRPCMenuItem = new MenuItem("Toggle RPC", (s, e) => ToggleRPC());
            var toggleAdsMenuItem = new MenuItem("Toggle Ads", (s, e) => ToggleAds());
            var exitMenuItem = new MenuItem("Exit", (s, e) => Exit());
            menu.MenuItems.Add(toggleRPCMenuItem);
            menu.MenuItems.Add(toggleAdsMenuItem);
            menu.MenuItems.Add(exitMenuItem);
            trayIcon.ContextMenu = menu;
        }
    }
}

Turns out I had a method that ran before CreateTrayIcon();原来我有一个在CreateTrayIcon(); : :

StartWebServer().Wait();

Which blocks the rest of the code.这会阻止其余代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM