Even a chimp can write code

Sunday, December 04, 2005

How to: Reference a file embedded within a DLL in WPF

In Windows Presentation Foundation, you can access a file embedded within a reference assembly via markup and in code using a special Component qualifier in your relative URI.

Suppose you have a reference assembly (declared as Reference in your project), which contains an image file that you'd like to access. In markup, you can write:

<Image Source="/MyReferenceAssembly;Component/MyImage.jpg" />

The Component qualifies MyReferenceAssembly as being a reference assembly. The reference assembly must always be the first segment in such a relative URI. The example above is a common short form of the actual usage. The full form involves the assembly's strong name in order to unambiguously address a specific assembly at runtime. You can extend this concept to assemblies in the Global Assembly Cache (GAC) as well.

The actual long form of this URI syntax involves referencing the assembly in a URI segment with the following syntax:

  • Each parameter within this URI segment is delimited by a semi-colon (;)

  • ComponentDisplayName is the display name of the assembly or library, and is mandatory

  • The optional VersionNumber is a dotted-decimal string prefixed with a v.

  • The optional PublicKeyToken is the hexadecimal form of the public key token. In string form, it is represented in 16 characters

  • The keyword Component is mandatory when referencing assemblies or libraries

  • A true strongly named component requires all of the above to be specified.

None of the above parameters are required to be case-sensitive. Each of the following represents a legal use of this syntax:

<Image Source="/MyLibrary;v1.0.0.1;9b35aa32c18d4fb1;Component/MyImage.jpg" />

<Image Source="/MyLibrary;Component/MyImage.jpg" />

<Image Source="/MyLibrary;v1.0.0.1;Component/MyImage.jpg" />

<Image Source="/MyLibrary;9b35aa32c18d4fb1;Component/MyImage.jpg" />

<Image Source="/MyLibrary;9B35AA32C18D4FB1;component/MyImage.jpg" />


Email this | Bookmark this


Post a Comment | Home | Inference: my personal blog