Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
 Resource Files
Silverlight 2
Resource Files
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Applications often use files that contain non-executable data. For example, Silverlight applications often use image, video, and audio files. Silverlight applications can also use other types of non-executable data including XML and XAML files. In Silverlight, non-executable data files are referred to as application resources and Silverlight provides both markup and code mechanisms to use them.

Note:

Application resources are different than Silverlight resources. Silverlight resources refer to Silverlight features such as styles and templates that are used together with resource dictionaries.

As with library assemblies, application resources can be deployed both in-package and on-demand. The deployment approach you choose depends on whether your application can run without a particular application resource. From a packaging perspective, there are three types of application resources in Silverlight:

  • Resource files

  • Content files

  • Site of origin files

This topic contains the following sections.

Note:

This topic describes development by using MSBuild. For Visual Studio development, see The Silverlight Preview Window.

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:

XAML
<!-- Image resource file embedded in application assembly -->
<Image Source="EmbeddedInApplicationAssembly.png" />

C#
// 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:

XAML
<!-- Image resource file embedded in application assembly -->
<!--<Image Source="EmbeddedInApplicationAssembly.png" />-->

C#
// 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:

XAML
<!-- Image resource file embedded in library assembly -->
<Image Source="/SilverlightLibrary;component/EmbeddedInLibraryAssembly.png" />

C#
// 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.

Content files are non-executable data files that are deployed in-package. You add a content file to your application by configuring it in the MSBuild project file by using the Content attribute, as shown here:

<Project ... >

  <!-- Application Configuration -->
  <PropertyGroup>
    ...
  </PropertyGroup>
  ...
  <!-- An image content file that is embedded into the application package -->
  <ItemGroup>
    <Content Include="IncludedInApplicationPackage.png" />
  </ItemGroup>
  ...
</Project>

This setting causes the content file to be embedded in the application package, and can be located by using a relative URI. The following markup and code-behind equivalents demonstrate how to access the ColocatedInApplicationPackage.png content file by using a relative URI:

XAML
<!-- Image content file in application package -->
<Image Source="/IncludedInApplicationPackage.png" />

C#
// Image content file in application package
Image img2 = new Image();
img2.Source = new BitmapImage(new Uri("/IncludedInApplicationPackage.png", UriKind.Relative));

Since the content file is not embedded in the application assembly, it will not be relative to the code in the assembly that is referencing it because it is at neither the same level as the code nor below it. Instead, it is at the same level in the application package as the assembly in which the code is running. Consequently, you must use the leading forward-slash to express the appropriate relativity.

On first appearances, resource files and content files are the same. This is true when an application package has only one assembly (for example, the application assembly). However, if there are multiple assemblies, in-package or on-demand, that share an application resource file, the application resource file can be shared by configuring it as an in-package content file.

Site of origin files are non-executable data files that published alongside the application package at the application’s site of origin, typically a Web site. Site of origin files are downloaded on-demand. Unlike library assemblies, you do not have to write the code to download the files on-demand.

You add a site of origin file to your application by configuring it in the MSBuild project file by using the None attribute, shown here:

<Project ... >

  <!-- Application Configuration -->
  <PropertyGroup>
    ...
  </PropertyGroup>
  ...
  <!-- An image site of origin file -->
  <ItemGroup>
    <None Include="OnDemandAtSiteOfOrigin.png">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  ...
</Project>

The None attribute tells MSBuild that the site of origin file should not be embedded in the application assembly or the application package. When you build the project, you therefore have to do the work of copying the site of origin file to the site of origin. By using the CopytoOutputDirectory property with a value of Always configures MSBuild to do this very thing for you automatically when the project is built.

An important thing to note is that a site of origin file is downloaded on-demand because it is not deployed with the application package. Therefore, the same considerations for application resources can be made as with in-package versus on-demand assemblies with regard to where application resources should be deployed. If an application cannot run without an application resource file, it should be either a resource file or a content file. Otherwise, you should deploy your application resource file as a site of origin file to save users the download cost until they need the application resource file.

The following markup and code-behind equivalents demonstrate how to access the OnDemandAtSiteOfOrigin.png site of origin file by using a relative URI:

XAML
<!-- Image resource file at the site of origin -->
<Image Source="/OnDemandAtSiteOfOrigin.png" />

C#
// Image file at site of origin
Image img4 = new Image();
img4.Source = new BitmapImage(new Uri("/OnDemandAtSiteOfOrigin.png", UriKind.Relative));

Since the site of origin file is not embedded in the application assembly or the application package, it will not be relative to the code. However, files that are at the site of origin are considered the equivalent of content files from a referencing perspective. This means that you use the leading forward-slash to express the appropriate relativity.

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker