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

一世执手 提交于 2020-05-24 04:27:36

问题


I have a WPF program with two libraries

now inside I have a gray resource background both in the main program and HelperLib. But additionally in the HelperLib I have a red background I want to use.

Now when I want to change the background of a window with:

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

but I get an image not found exception on the RED image but not on the other one. I suspect that, when using the gray gradient it's not using the one in the lib but the one in the main program since I see that the resource is related to the assembly not to the project. Both images have the same properties as in picture:

Thanks for helping


回答1:


You would have to use a Resource File Pack Uri with the name of the referenced assembly:

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


来源:https://stackoverflow.com/questions/34763291/using-a-resource-image-in-a-library-different-from-the-main-project

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