gpt4 book ai didi

wpf - 从后面的代码或 XAML 中的 NotifyIcon 控件设置图像

转载 作者:行者123 更新时间:2023-12-04 16:24:14 25 4
gpt4 key购买 nike

我正在使用来自 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,而不是在 Code Behind 中吗?如果可以的话,我该怎么做?

提前致谢!

最佳答案

System.Drawing.Icon 不支持用于 WPF 资源的 pack:// 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
    };

关于wpf - 从后面的代码或 XAML 中的 NotifyIcon 控件设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5758581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com