在与主项目不同的库中使用资源图像

Using a resource image in a library different from the main project

我有一个包含两个库的 WPF 程序

现在我在主程序和HelperLib中都有一个灰色的资源背景。但另外在 HelperLib 中我有一个我想使用的红色背景。

现在,当我想更改 window 的背景时:

 switch (bubbleType)
  {
    case eBubbleType.ERROR:
      bw.btText.Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/Resources/Images/gradientWallpaper_RED.jpg"))); <-----I want to use this one 
      break;

    default:
      bw.btText.Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/Resources/Images/gradientWallpaper.jpg")));
      break;
  }

但我在红色图像上发现图像未找到异常,但在另一张图像上没有。 我怀疑,当使用灰色渐变时,它不是使用库中的那个,而是主程序中的那个,因为我看到资源与程序集相关,而不是与项目相关。 两个图像都具有与图片相同的属性:

感谢您的帮助

您必须使用带有引用程序集名称的 Resource File Pack Uri

new Uri(
  "pack://application:,,,/HelperLib;component/Resources/Images/gradientWallpaper_RED.jpg")