Resource files are non-executable data files that are embedded in either an application assembly or a library assembly. A resource file is deployed in-package if the assembly it is embedded in is deployed in-package. Likewise, if an assembly is deployed on-demand, any resource files that are embedded into it are also deployed on-demand.
You add a resource file to your application by configuring it in the MSBuild project file by using the Resource attribute, shown here:
<Project ... >
<!-- Application Configuration -->
<PropertyGroup>
...
</PropertyGroup>
...
<!-- An image resource file that is embedded into the assembly -->
<ItemGroup>
<Resource Include="EmbeddedInApplicationAssembly.png" />
</ItemGroup>
...
</Project>
This setting causes the resource file to be embedded into the assembly, and can be located by using a relative URI. The following markup and code-behind equivalents demonstrate how to access the EmbeddedInApplicationAssembly.png resource file by using a relative URI:
<!-- Image resource file embedded in application assembly -->
<Image Source="EmbeddedInApplicationAssembly.png" />
// Image resource file embedded in application assembly
Image img1 = new Image();
img1.Source = new BitmapImage(new Uri("EmbeddedInApplicationAssembly.png", UriKind.Relative));
Note that the URI reference is relative to the calling code. Depending on where the code is, the resource file that you are looking for may not be relative to (at the same level as or below) the code. In this case, you can specify the URI to be relative to the root of the assembly by using a leading forward-slash in your URI. For example, the following markup and code-behind demonstrate how to access a resource relative to the root:
<!-- Image resource file embedded in application assembly -->
<!--<Image Source="EmbeddedInApplicationAssembly.png" />-->
// Image resource file embedded in application assembly
Image img5 = new Image();
img5.Source = new BitmapImage(new Uri("/EmbeddedInApplicationAssembly.png", UriKind.Relative));
If your code is executing in the application assembly, and has to get a resource file that is embedded in a referenced assembly, you have to use a different type of URI, as shown here:
<!-- Image resource file embedded in library assembly -->
<Image Source="/SilverlightLibrary;component/EmbeddedInLibraryAssembly.png" />
// Image resource file embedded in library assembly
Image img3 = new Image();
img3.Source = new BitmapImage(new Uri("/SilverlightLibrary;component/EmbeddedInLibraryAssembly.png", UriKind.Relative));
The /SilverlightLibrary;component/ section of the URI instructs Silverlight to look in the referenced assembly for the desired resource and the leading forward-slash tells Silverlight that the referenced assembly is relative to the assembly that the code is currently running in.