Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the correct pack:// URI Format

Tags:

c#

wpf

xaml

I have a WPF class Library, containing a Folder "Images" that contains an Image. In a WPF application I wont to refer to the image in that Folder using

<Image Name="image" Grid.Row="0" Source="pack://application:,,,/ImageService.dll;Component/Images/image.jpg"/>

The image ist marked as "Ressource" and I also added it to the resources of the class library Project. After some searching I found out, that I have to copy the dll into the exe directory.

During designtime the image is displayed correctly. After launching the executable the source property of the image is null. No error message and no hints in the output window.

It seems, that the Format of the pack uri is incorrect, so the runtime refers to a wrong place. Is there a way to specify the assembly name relative to the executable, using "....\AssemblyName.dll" or absolute to file systems root.

Unfortunately I cannot assign the imagesource via code, because all the content goes through XAMLWriter - XAMLReader construct from the dll to the executing assembly.

like image 742
Matthias Fuchs Avatar asked Apr 19 '14 15:04

Matthias Fuchs


1 Answers

You don't have to copy image to output directory. Make sure you mark the Build Action as Resource and set Copy to Output Directory to false.

Also, you can skip assembly name in pack URI in case XAML and image resides in same assembly. In case they resides in different assembly, you have to give only assembly name (remove dll from path).

<Image Source="pack://application:,,,/ImageService;Component/Images/image.jpg"/>
like image 109
Rohit Vats Avatar answered Oct 22 '22 17:10

Rohit Vats