Set image in NotifyIcon control from code behind or in XAML

一曲冷凌霜 提交于 2019-12-03 21:27:51

问题


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:


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


来源:https://stackoverflow.com/questions/5758581/set-image-in-notifyicon-control-from-code-behind-or-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!