URI for local file relative to assembly

For example, I have the following project structure:

Root\Core\Application.exe Root\Modules\Assembly.dll Root\Modules\Icons\Icon.png 

My Application.exe downloads Assembly.dll. Assembly.dll includes a WPF UserControl with an Image control. Icon.png is not an embedded resource, it is a local file (Build: Content).

I tried all possible URIs in the Source-Property for Image Control, but never shows the image: - (

 <Image Source="Icons\Icon.png" /> <Image Source="pack://application:,,,/Icons\Icon.png" /> <Image Source="pack://application:,,,/Assembly;component/Icons/Icon.png" /> 

and etc.

What should a URI look like?

+7
source share
1 answer

This statement means that you cannot reference the content files in your assembly ... (therefore, only the content files specified in the application can be resolved using "application: //").

http://msdn.microsoft.com/en-us/library/aa970069(v=vs.85).aspx

Content files in referenced assemblies are not included because they are not supported by WPF. Package URIs for embedded files in referenced assemblies are unique because they include both the referenced assembly name and the component suffix. Pack URIs for the site source files are unique because they use the only package URIs that use the siteoforigin: /// property.

You can find somewhere "siteoforigin" that refers to the place where your .exe works.

 Source="pack://siteoforigin:,,,/../Modules/Icons/Icon.png" 

or

 Source="pack://siteoforigin:,,,/Icons/Icon.png" 

Although this may not support the relative path.

Give this snapshot also:

 Source="../Modules/Icons/Icon.png" 

And this:

 Source="Icons/Icon.png" 

One tip when experimenting with a package URI is to create them using PackUriHelper so that you match the correct syntax / semantics.

See some related posts:


Another idea ....

You can define your own MarkupExtension, which helped create an absolute path to your png files.

You will have a global configuration parameter that was set just before loading your DLL using LoadFrom.

Then you should use XAML to expand:

 Source={local:MyMarkupExtensionPathBuilder Icon.png} 

Use this as a starting point ... in your case you do not want to use "application: //", though ... you want to build Uri as a direct absolute "file" of type URI path, for example. "C: \ Program Files \ myapplication \ Root \ Modules \ Icons \ icon.png".

You can force MarkupExtension to directly return a BitmapSource (or just get it to return a string ... which TypeConverter then enables BitmapSource).

(you can also do something similar with the binding that used the converter to combine the two parts of the path, or make your ViewModel concatenated if you use one ... the markup extension is a tidier way to do this, though)

+5
source

All Articles