为什么 WPF NotifyIcon Icon 属性设置器抛出异常?

发布于 2024-09-03 03:01:31 字数 321 浏览 1 评论 0原文

我在 WPF 中使用 NotifyIcon 时遇到问题,第二行抛出异常。我似乎找不到使用资源中的图标文件的方法,任何人都可以提供帮助。

notifyI = new NotifyIcon();
notifyI.Icon = new Icon("Power.ico");
notifyI.Text = "Shutdown Timer";
notifyI.Visible = true;
notifyI.MouseDoubleClick += new
System.Windows.Forms.MouseEventHandler(notifyI_MouseDoubleClick);

I'm having trouble with a NotifyIcon in WPF, the second line is throwing an exception. I can't seem to find a way to use the icon file I have in the resources, can anyone help.

notifyI = new NotifyIcon();
notifyI.Icon = new Icon("Power.ico");
notifyI.Text = "Shutdown Timer";
notifyI.Visible = true;
notifyI.MouseDoubleClick += new
System.Windows.Forms.MouseEventHandler(notifyI_MouseDoubleClick);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2024-09-10 03:01:31

Icon(string) 构造函数在磁盘上的文件中查找图标文件,而不是在资源中查找。考虑改用 Icon(Stream) 构造函数。

或者使用“项目 + 属性”、“资源”选项卡、“添加资源”按钮上的箭头、“添加现有文件”。选择您的 .ico 文件。然后你会像这样使用它:

 notifyI.Icon = Properties.Resources.Power;

The Icon(string) constructor looks for a file on disk for the icon file, it doesn't look in a resource. Consider using the Icon(Stream) constructor instead.

Or use Project + Properties, Resource tab, arrow on Add Resource button, Add Existing File. Select your .ico file. Then you'd use it like this:

 notifyI.Icon = Properties.Resources.Power;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文