1

I have a WPF UserControl, which is using some PNG bitmap resources as part of it's UI. Currently, those files are referenced with Build Action: Content and a Copy to Output Directory: Do not copy. Elements on this UserControl are using following URI to access the resource: pack://application:,,,Resources/ui/Cross.png which works fine in designer.

The user control is then used in a Windows Forms application through ElementHost control at which point all image links stop working. The UserControl is in it's own assembly and Windows Forms application is referencing that assembly.

I have also tried changing the URI to this: pack://application:,,,Assembly.Name;component/Resources/ui/Cross.png

I have further tried reading this to no help.

The only option I have left is to include code to manually set ImageSource of all Image elements on the user control, which would require a lot of work. I feel like there must be a way to resolve this better.

masiton
  • 147
  • 2
  • 10

1 Answers1

2

Build Action: Content and a Copy to Output Directory: Do not copy

That combination does not work. Content requires that the file is copied.

However, you should use Build Action Resource. And your Pack URI misses a slash after ,,,.

It should be

pack://application:,,,/AssemblyName;component/Resources/ui/Cross.png

provided that the image file is part of your Visual Studio Project, located in a subfolder named ui in a project folder named Resources.

See Resource File Pack URIs for details.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • The mentioned has no effect on whether the images load or not. It is a known issue with hosted controls, I'm looking for an advise how to resolve this. No combination of build action and uri formatting will resolve this. – masiton Nov 21 '18 at 11:02
  • Not sure what you are trying to say, but Content and Do not copy will definitely not work together. Use Build Action Resource and a Referenced Assembly Resource File Pack URI. – Clemens Nov 21 '18 at 11:04
  • I'm trying to say that your proposed changes do not resolve the situation either. You can't just play around with build action and tune the URI link and expect that a WPF control hosted in Windows Forms app will suddenly work and URI resource links will suddenly spring to life. That entire feature doesn't work in hosted environment as explained by Dr. WPF in the link provided in the question. – masiton Nov 21 '18 at 11:07
  • Just tried it, and it works perfectly. Hosted a WPF UserControl in an ElementHost in a WinForms app. UserControl has an Image element with a BitmapImage loaded from a full Pack URI. You are not supposed to "play around" with Build Action and other resource file settings. Just set them correctly. Also make sure your resource file path is correct, i.e. there is a folder "Resources" with a subfolder "ui". – Clemens Nov 21 '18 at 11:25
  • Also double check that you used the correct assembly name in the Pack URI. Do not use a namespace name. – Clemens Nov 21 '18 at 11:33
  • I noticed the question was misleading. The User Control is in a different assembly. Hence the resource uri doesn't work. I have updated the question. – masiton Nov 21 '18 at 11:51
  • You mean in a in a different assembly than the image resource files? That also works for me. Even if only the main application references the resource assembly. – Clemens Nov 21 '18 at 11:57
  • This solution works for images referenced from elements directly, as stated in the question, so I am marking this as a solution. The important bit here being the slash character before the Assembly name. The *only* working way of referencing the image is `pack://application:,,,/AssemblyName;component/Resources/ui/Cross.png` – masiton Nov 21 '18 at 11:59