1

I am posting this and will answer it because of the lack of working, marked correct, solutions to this on SO.

The issue is when you create custom controls or external libraries that use images, the images may not show up in the program that uses the library.

In my specific case I have user controls that use a custom control base class.

For some reason the image would appear in the design view of the usercontrol, and external program's design window. however, on running it would not have the image displayed in the external program window.

I tried all of the following from other solutions on SO:

  • using the pack://,,, command in the xaml of the DP (works in design views)
  • referencing the individual xaml files in the external program (this is a bad idea from a OO & SOLID point of view)
  • Using ../ before the URI string (works in design views)
  • Using EmbeddedResource for the build of the Images
  • Creating a converter to process the string (unnecessary and adds extra garbage to the system as well as not working very well)
  • Creating extra dependency properties on the custom control (Messy and a bad idea from a binding point of view)
  • Using assembly;component/[folder name]/[image name] (works in design views)
  • Using [folder name/image name] (works in design views)
  • Changing the assemblyinfo.cs file and the static constructor of the custom class to include the Generic.xaml file.
Slipoch
  • 750
  • 10
  • 23

1 Answers1

1

So some of the answers were so close, I only discovered it because I started using Snoop WPF, get it here: Snoop @Github (I am not affiliated with them in any way, I only just discovered it)

I could use this program to drill down into the program and find the error that was occurring, the URI string seemed to be having issues in being interpreted. This ruled out a bunch of stuff in the list above.

Eventually I found the answer on The MSDN forums by searching for the specific errors, you have to use: /[assembly];component/[folder name]/[image name], please note the start of the string is a /, which was the only thing missing from one of my attempts.

Even using the pack command in the string with the / before the assembly did not work.

So for a library project called myLibrary, a custom control called MyControl, an image folder called resources, and an image called myTestImage.png inside that folder. the call for the custom control would be:

<local:MyControl ImageSource="/myLibrary;component/resources/myTestImage.png" />
Slipoch
  • 750
  • 10
  • 23
  • Two things: 1) it's not "the pack command" -- rather, it's the [pack URI](https://docs.microsoft.com/en-us/dotnet/framework/wpf/app-development/pack-uris-in-wpf) scheme, and it has a specific format. The correct usage in your control would be `pack://application:,,,/myLibrary;component/resources/myTestImage.png` **not** `pack://,,,/myLibrary;component/resources/myTestImage.png`, which is what it sounds like you tried. 2) If you look at the MSDN forum post that you linked to, you can see that they in fact got *their* answer from the StackOverflow question which this is basically a duplicate of. – Herohtar Nov 15 '19 at 03:37
  • I actually tried both of those and neither worked.Yes indeed this is the same result, however due to the way it is worded, when I searched I could not get it to come up. I only found it due to the MSDN forum where they mention more of the error. – Slipoch Nov 15 '19 at 04:39