【问题标题】:Set image in NotifyIcon control from code behind or in XAML从后面的代码或 XAML 中的 NotifyIcon 控件设置图像
【发布时间】:2011-04-22 18:18:28
【问题描述】:

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

提前致谢!

【问题讨论】:

    标签: wpf wpf-controls notifyicon


    【解决方案1】:

    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
      };
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多