My WPF UserControl can't locate resources when I use it with a project other than the one it was built in

Viewed 339

So I built a WPF user control as part of a larger solution. I'm building another app where this user control would work nice. I added a reference to the DLL from the new app, wired it up and compiled. The window loads and I can see the image that it's telling me it can't find. After the main window displays and the user control is populated, an exception is thrown saying...

System.IO.IOException: 'Cannot locate resource 'resources/nosortnofilter.png'.'

The user control is a DataGrid with some extensions added to it. The column it threw on was "id". As you can see in the image, the red arrow shows the nosortnofilter.png image being displayed in all columns. So why is it throwing this exception?

Exception

The line of code it throws on is here.

If ImageName = "" Then ImageName = "NoSortNoFilter"
img.Source = New BitmapImage(New Uri("pack://application:,,,/Resources/" & ImageName & ".png"))

enter image description here

So it all looks good from my perspective. Hoping someone can see what I'm not seeing.

EDIT: Found a solution. This works. But it still doesn't answer the questions why the original pack:// formatted URI only worked with the original solution.

img.Source = New BitmapImage(New Uri($"Resources/{ImageName}.png", UriKind.Relative))

EDIT: Thanks to rfmodulator for giving me the correct URI for DLL's.

img.Source = New BitmapImage(New Uri("pack://application:,,,/AdvancedSortFilterDataGrid;component/Resources/" & ImageName & ".png"))
1 Answers

This is the URI of a resource in the Application's assembly:

pack://application:,,,/Resources/[RESOURCENAME]

To get a resource in a DLL, the URI looks more like this:

pack://application:,,,/[DLLNAME];component/Resources/[RESOURCENAME]

For details, see Pack URIs in WPF.

Related