任务栏通知无效:C#

时间:2014-08-01 10:39:55

标签: c# notifications

我是C#的新手。我想写一个程序来显示一个简单的通知。在谷歌搜索并看到几个stackoverflow答案后,我终于写了一个程序。但它不起作用。

代码:

public class SysTrayApp : Form
{
    [STAThread]
    public static void Main()
    {
        Application.Run(new SysTrayApp());

    }

    private NotifyIcon trayIcon;
    private ContextMenu trayMenu;

    public SysTrayApp()
    {

        trayMenu = new ContextMenu(); // Create a simple tray menu with only one item.
        trayMenu.MenuItems.Add("Exit", OnExit);

        trayIcon = new NotifyIcon();
        trayIcon.Text = "MyTrayApp";
        trayIcon.Icon = new Icon("C:\\Users\\Name\\Desktop\\test.ico", 40, 40); //origin, width, height

        trayIcon.ContextMenu = trayMenu;

        trayIcon.BalloonTipTitle = "MyTitle";
        trayIcon.BalloonTipText = "This is sample message ";
        trayIcon.ShowBalloonTip(30000);
        trayIcon.Visible = true;
    }

    protected override void OnLoad(EventArgs e)
    {
        Visible = false; // Hide form window.
        ShowInTaskbar = false; // Remove from taskbar.

        base.OnLoad(e);
    }

    private void OnExit(object sender, EventArgs e)
    {
        Application.Exit();
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            // Release the icon resource.
            trayIcon.Dispose();
        }

        base.Dispose(isDisposing);
    }

}

它在任务栏中显示图标,但不显示通知。 我的要求与this question相同。

1 个答案:

答案 0 :(得分:-1)

问题是显示BallonTip的代码驻留在表单的构造函数中。您必须将 ShowBallonTip()调用放置在表单'costructed'之后运行的方法中。