Set image in NotifyIcon control from code behind o

2019-02-15 09:16发布

i am using the NotifyIcon from WindowsForms because in WPF we don't have such control, but the one from WinForms works fine, my problem is only setting an image as icon in the NotifyIcon when the image is in the Project.

I have the image in a folder called Images in my Project, the image file calls 'notification.ico'.

Here is my NotifyIcon:

System.Windows.Forms.NotifyIcon sysIcon = new System.Windows.Forms.NotifyIcon() 
{
    Icon = new System.Drawing.Icon(@"/Images/notification.ico"),
    ContextMenu = menu,
    Visible = true
};

What i am doing wrong here?

And can i create my NotifyIcon in XAML instead in Code Behind? If it's possible, how can i do it?

Thanks in advance!

1条回答
SAY GOODBYE
2楼-- · 2019-02-15 09:34

System.Drawing.Icon doesn't support the pack:// URI scheme used for WPF resources. You can either:

  • include your icon as an embedded resource in a resx file, and use the generated property directly:

    System.Windows.Forms.NotifyIcon sysIcon = new System.Windows.Forms.NotifyIcon() 
    {
        Icon = Properties.Resources.notification,
        ContextMenu = menu,
        Visible = true
    };
    
  • or load it manually from the URI like this:

    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
    };
    
查看更多
登录 后发表回答