我正在使用 WindowsForms 中的 NotifyIcon,因为在 WPF 中我们没有这样的控件,但 WinForms 中的控件工作正常,我的问题只是当图像位于项目中时将图像设置为 NotifyIcon 中的图标。
我的项目中有一个名为 Images 的文件夹中的图像,该图像文件名为“notification.ico”。
这是我的通知图标:
System.Windows.Forms.NotifyIcon sysIcon = new System.Windows.Forms.NotifyIcon()
{
Icon = new System.Drawing.Icon(@"/Images/notification.ico"),
ContextMenu = menu,
Visible = true
};
我在这里做错了什么?
我可以在 XAML 中而不是在代码隐藏中创建 NotifyIcon 吗?如果可以的话我该怎么做?
提前致谢!
System.Drawing.Icon
不支持pack://
用于 WPF 资源的 URI 方案。您可以:
-
将您的图标作为嵌入资源包含在 resx 文件中,并直接使用生成的属性:
System.Windows.Forms.NotifyIcon sysIcon = new System.Windows.Forms.NotifyIcon()
{
Icon = Properties.Resources.notification,
ContextMenu = menu,
Visible = true
};
-
或者从 URI 手动加载它,如下所示:
StreamResourceInfo sri = Application.GetResourceStream(new Uri("/Images/notification.ico"));
System.Windows.Forms.NotifyIcon sysIcon = new System.Windows.Forms.NotifyIcon()
{
Icon = new System.Drawing.Icon(sri.Stream),
ContextMenu = menu,
Visible = true
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)