Wpf 从代码隐藏或XAML中设置NotifyIcon控件中的图像

Wpf 从代码隐藏或XAML中设置NotifyIcon控件中的图像,wpf,wpf-controls,notifyicon,Wpf,Wpf Controls,Notifyicon,我使用的是WindowsForms中的NotifyIcon,因为在WPF中我们没有这样的控件,但是WinForms中的控件工作得很好,我的问题是当图像在项目中时,仅将图像设置为NotifyIcon中的图标 我的项目中有一个名为Images的文件夹中的图像,该图像文件调用'notification.ico' 这是我的通知图标: System.Windows.Forms.NotifyIcon sysIcon = new System.Windows.Forms.NotifyIcon() {

我使用的是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
不支持用于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
    };