Pete Shaw
Pete Shaw

Reputation: 23

Is pack notation preferrable for WPF images in same assembly

If I have an application that is never intended to be localized and the resources are in the same assembly, is there any advantage to use the pack notation when referring to images?

I currently have all the images in a Resource folder with their build action set to Resource in the same assembly. I'm referring to like this (which is working)

<Image Source="/Resources/Image1.png"/>

Upvotes: 0

Views: 243

Answers (1)

Michael Edenfield
Michael Edenfield

Reputation: 28338

Strictly speaking, you are already using a pack URI, you're just using a relative one.

The purpose of Pack URI notation, as explained in the MSDN documentation on the subject, is that it

provide[s] a consistent mechanism for identifying and loading these types of files

from various different locations, which can includes:

  • The current assembly.
  • A referenced assembly.
  • A location relative to an assembly.
  • The application's site of origin.

To provide this kind of consistency, the URI scheme specifies how to declare both absolute and relative URI:

By default, a relative pack URI is considered relative to the location of the markup or code that contains the reference. If a leading backslash is used, however, the relative pack URI reference is then considered relative to the root of the application.

If you are asking about the benefits of absolute vs. relative pack URIs then the question can only be answered by you. Absolute URIs have the benefit that they always resolve to the same place, even if you move the referencing XAML around. Relative URIs have the benefit that your XAML and it's related resources can be moved around as a unit without breaking the relative paths.

If you are sure that your referenced resources will always be in the same assembly as the XAML source, then relative paths are probably the way to go. If you think you might ever want to separate XAML from static resources, there is a good argument to be made that you should do it early and make it work, using absolute URIs.

Upvotes: 1

Related Questions