Resources are always a vital part for web application and here in Silverlight
application too it plays an important role for creating Rich internet
applications. In this context Resource means Images,audio,videos,object, which
can be styles or datatemplete used for creating rich application. Silverlight
does support various ways of storing resources and managing it. In this post, we
will discover the different options available.
The complete information on
resources will span across multiple posts ,where we will cover Binary Resource
(Resource Files ) and XAML Resource .
Various Ways of managing Resources
Resources can be managed by following ways
- Resource at server Side ,Loading on Demand
- Resource embedded inside XAP/Assembly
- Resources from external assemblies
The most important point ,we
should not confuse over Assembly Resource and XAML Resource although both termed
as resource .We can divide resource types in to two categories
- Files other than executable or Binary/Assembly Resource (Managed through
- Objects such as Styles,Templates etc.. or XAML Resource(Managed Through
Basically the scope of this post is to give you a
brief about Resource Files or Binary Resource or Assembly Resource .In this post
we will explore how these resource files can be stored and managed from
Silverlight .The subsequent post will focus on Resource Dictionary or XAML
Managing Resource Files
Before moving on the Resource
Files lets have quick look into some important terminologies we will come across
frequentlyApplication Package -
The files packaged as a
XAP file which includes the Silverlight assemblies and Application manifest
files also Resources if any.Application Root
location where XAP files reside in the deployed solution most of the time
- Stands for Uniform Resource Identifier ,
is the path to the the Resource.It can be either
|Exact location of the Resource for .eg. a qualified URL
||Location of file with respect to the application root (Where
Application package resides)For e.g. "/image1.jpg"|
|Used when files are other than application root
||A common scenario of storing resource with in the domain under
||Files must be inside the same application root folder where package
The application root is the folder where
the application package XAP file resides.So all the files must be with in the
same folder as XAP file .The relative URI does not look for content outside this
folder but its does support folder structure inside the application root.So we
can have our file image1.jpg inside Resource folder inside application root
Other than Uri there is BuildAction property for each files which
we will discover as we go through with a sample as follows.
As we know Silverlight application is
nothing but a package of files compressed and stored as single file.Any resource
added to the client side inside the Silverlight project will transfer be
transferred to client side based on the Mode of Deployment.Lets check with the
example as bellow ,
Mode Of Deployment , BuildAction
Here the most important
to note how the resource is configured for deployment by default the file's
BuildAction properties set as a Resource.Silverlight does offer various options
which can be handy at some time.
the resources can be be deployed with following options BuildAction as
with each option the application package and the URI
defers.Also make a note that in Silverlight project, although there are various
options but only above mentioned build actions can be
Content option add the file to
application package as loose resource .
in the project Change the file BuildAction as Content and Rebuild the
solution.The XAP file size is almost 42 KB which includes Image .
to locate the resource file you can follow either of the method mentioned
= new BitmapImage(new
This Option will add the resource with in the application package
inside the Silverlight assembly .
Rebuilding the project after changing the BuildAction type of image , the XAP
file size remain same 43 KB but the image is embedded inside the
MySilverlightApp.dll.You can mark the difference in size of assembly compared to
above content type.
locate the resource there should not be any leading trails
= new BitmapImage(new Uri("BananaTECH.jpg",
option neither add the resource to the application package nor to the
Silverlight assembly.You need to add resource to the server manually.
None build action the XAP file size reduced dramatically as the resource is not
embedded inside the package or assembly.The screenshots bellow shows the XAP
file with 4 KB of size.To make your deployed code working we need to place the
resource file inside the published project under clientbin.
= new BitmapImage(new Uri("BananaTECH.jpg",
Resource from an external assembly
Its wise to refactor
the resource into a separate assembly for reuse amongst the applications.The
best example of external assembly for resource is the theme libraries.The
Silverlight too support using external Silverlight assemblies in projects .Lets
create a Silverlight assembly project ,
adding a reference to the Silverlight project will allow you to use the resource
file inside the application.
can be used by using following syntax - /assemblyName;component/resourceLocation.
For example in
this project the image file from MyResources
assembly can be fetched
with URI "/MyResources;component/Rupees.jpg".
The component here
is the keyword.?
Hope this post will give you a clear idea
about Resource Files .The subject Resource is not complete yet ,here we
discussed only about Files/ Binaries how over lot more to discuss and share.In
my subsequent posts i will try to cover more about this topic.Send me you
suggestions and queries.
Download sourcecode ResourcesInSL