URI of absolute and relative packets

My question is: am I reading MSDN incorrectly OR incorrect MSDN? I am trying to reference an image resource through a relative URI.

The MSDN page called Pack URIs in WPF clearly states that:

"Resource file in a subfolder of the local assembly" = " /Subfolder/ResourceFile.xaml "

Table 2: Relative URIs in the markup (here http://msdn.microsoft.com/en-gb/library/aa970069.aspx ).

So, I created new WPF / .NET 4.0 projects in Vs2010 (v10.0.40219.1 SP1Rel) for testing.

Startup Project: TryUri App.xaml MainWindow.xaml Custom Ctrl Project: TryUri.CCL Icons/folder.png <-- Marked As Resource! Views/TestUC.xaml 

In TestUC.xaml:

 <Canvas> <Border> <Image Source="/Icons/folder.png" Height="Auto" Width="Auto" /> </Border> </Canvas> 

In MainWindow.xaml:

 // xmlns:uc="clr-namespace:TryUri.CCL.Views;assembly=TryUri.CCL" <uc:TestUC /> 

Result: the image does not appear!

When I change the path to "../Icons/folder.png" It works.

If I copy Icons / folder.png to the TryUri.CCL / Views / folder, then "Icons / folder.png" works.

However, I can never get the leading slash to work the same as in "/Icons/folder.png," as stated by MSDN.

+6
source share
3 answers

The reason when working as a separate assembly is that your Relative Pack URI should start with ../ or not have a backslash due to the way relative URIs are used, from MSDN (emphasis mine) :

Absolute and Relative URI Packages

...

If a leading backslash is used , the relative URI package reference is considered relative to the application root .

Therefore, if you want the images to remain relative to your reference assembly, simply omit the leading slash or use ../ if in a subfolder.

See the image below for an example of what it looks like at runtime:

Absolute vs. Relative Pack URIs

+12
source

The answer is: a leading slash in a relative path is always allowed in the main assembly, not in the assembly in which the markup is located.

To make it work as MSDN claims, I would need to save all the icons in the "Icons /" folder in the main assembly, and not in a special DLL control panel.

If you decide to save the resources in the dll assembly in the "Icons /" folder, you should refer to them along the path relative to the markup you are working on (ie " ../Icons/folder.png ") OR you must be qualified path with the name of the assembly (ie " /AssemblyName;component/Icons/folder.png ")

+7
source

Just replace <ApplicationName> with the name of your application and this should work

 Source="/<ApplicationName>;component/Icons/folder.png" 
+4
source

All Articles