为了未来的编码员:
[timeout] 参数自 windows vista 起已弃用
请参阅:C# NotifyIcon 显示气球参数已弃用
因此,您不妨将 0 放入 > Windows Vista 的参数中。更糟糕的是,对链接答案的评论表明,这些气球的替代品,吐司通知,仅在 Windows 8 中引入。因此,对于可怜的旧 Windows 7 落在两个凳子之间,Vista < 7 < 8,我们似乎处于不管 Windows 想把那个气球放在那里多长时间都行不通!我注意到它最终确实会消失,但是经过一些经验测试后,我很确定该参数确实被忽略了。
因此,在上述答案的基础上,特别是采用@jlmt 在评论中建议的 lambda 函数,这里有一个适用于 Windows 7 的解决方案:
//Todo: use abstract factory pattern to detect Windows 8 and in that case use a toastnotification instead
private void DisplayNotificationBalloon(string header, string message)
{
NotifyIcon notifyIcon = new NotifyIcon
{
Visible = true,
Icon = SystemIcons.Application
};
if (header != null)
{
notifyIcon.BalloonTipTitle = header;
}
if (message != null)
{
notifyIcon.BalloonTipText = message;
}
notifyIcon.BalloonTipClosed += (sender, args) => dispose(notifyIcon);
notifyIcon.BalloonTipClicked += (sender, args) => dispose(notifyIcon);
notifyIcon.ShowBalloonTip(0);
}
private void dispose(NotifyIcon notifyIcon)
{
notifyIcon.Dispose();
}
笔记
- 我在里面放了一个 TODO 来为 Windows 8 编写另一个实现,因为现在人们在 Windows 7/8 上的比例是 50/50,所以支持更新的功能会很好。我猜想为多个版本的 Windows 编写此代码的其他任何人都应该这样做,理想情况下。或者干脆停止支持 7 并切换到使用 ToastNotification。
- 我特意在一个函数中定义了处理,这样我就可以调试并验证断点确实被命中了。