1

I have a question regarding Pack Uri.

I created a WPF .NET 4.5 application. I would like to make it quite modular to be open for 3rd party plugins; so i split it up in three projects.

My Configurator executable will be the main entrance point. This configurator assembly references a Types.dll, which contains most of my base logic. For my guided user interface, I use a third project, called UserInterface.dll

Now my types.dll contains an image file, which I would like to use as resource in my UserInterface.dll.

If I try to call it e.g. using:

   Image img = new Image();
   img.Source = new BitmapImage(new Uri("/Types;component/Resources/myPicture.png", UriKind.Relative);

The image will not show up, because it cannot not found. My executing assembly is UserInterface.dll, but Types.dll is referenced in Visual Studio. Same effect using absolute Uris (application,,,:).

I did a work around defining my image as Resource (Project Property) and load the Drawing.Bitmap, convert it to an ImageSource and assigning it to my image. But I think it is not as performant as loading it directly via ImageSource.

So my question is: How can I load a resource in a called assembly from another assembly, when both are not the main application?

Thanks for your help.

EDIT:

The images's build action is set to Resource.

Hans Bäuml
  • 229
  • 3
  • 15

2 Answers2

2

Use more verbose definition of Pack URI for referenced assembly. Refer to Referenced Assembly Resource File section under Pack URI in WPF.

It will look like this:

img.Source = new BitmapImage(new Uri("pack://application:,,,/Types;component/Resources/myPicture.png");

Also make sure Build Action for myPicture.png is set to Resource.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Thanks for your reply. I did it verbouse and non verbouse. The image's build action is set to Resource. I'll add that information to my question. As I understand it, the application:,,, buzzword will get the entry assembly and looking in the dll Types, which has to be in the same folder, for this resource. So normally it should be found, but it is not and I wonder why. – Hans Bäuml Oct 02 '14 at 09:28
  • This works in small sample which I have tried. Can you try reproducing it in small sample and see if problem persist. If yes please post the sample code somewhere on the cloud so that we can have a look. – Rohit Vats Oct 02 '14 at 09:49
0

Short Answer:

The answer may be, that there are some issues with .NET 4.5.1 or the error was somewhere else. Switching back to .NET 4.5 worked for me. Referencing a resource out of a referenced dll works using uri, too. If I find out, what I did wrong I'll update this.

Coming to the "solution":

Thank's a lot for your help, Rohit, the application works now. I don't know how I did it, but I did it :) Thank you for taking the time making a sample solution, should have done this before posting here...

I wonder where the problem came from. I built a small sample, as you suggested and got the same results as you did. I switched back to my application to see what I did different there. First I checked all my References on the WPF assemblies. They were all like in my new sample project. I switched my application back to .NET 4.5 (from .NET 4.5.1) and then it worked in my app, too. After that I switched back to .NET 4.5.1 to see, if I get the error again - but it still works. It even could not be a typo, as I am using the same code (commented it out) both times. So what can it be, that caused this problem? Perhaps you have an idea?

Hans Bäuml
  • 229
  • 3
  • 15
  • Most likely only `rebuild` was missing. Switching frameworks might caused the same like a rebuild. See https://stackoverflow.com/a/11948876/3090544 – MarkusEgle Sep 27 '19 at 07:16