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
In Silverlight
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
Resource Files)
- Objects such as Styles,Templates etc.. or XAML Resource(Managed Through
Resource Dictionary)
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
Resource.
Managing Resource Files
Before moving on the Resource
Files lets have quick look into some important terminologies we will come across
frequently
Application Package - The files packaged as a
XAP file which includes the Silverlight assemblies and Application manifest
files also Resources if any.
Application Root - The
location where XAP files reside in the deployed solution most of the time
ClientBin
URI - Stands for Uniform Resource Identifier ,
is the path to the the Resource.It can be either
| Absolute URI |
Relative URI |
| Exact location of the Resource for .eg. a qualified URL
(www.manaspatnaik.com/image1.jpg) |
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
application root |
|
Files must be inside the same application root folder where package
exists. |
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
folder.
Other than Uri there is BuildAction property for each files which
we will discover as we go through with a sample as follows.
Resource File
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.

Generally
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
used.
Content -:
Content option add the file to
application package as loose resource .

Here
in the project Change the file BuildAction as Content and Rebuild the
solution.The XAP file size is almost 42 KB which includes Image .
Here
to locate the resource file you can follow either of the method mentioned
,
XAML :
?
Code :
| 1 |
image1.Source
= new BitmapImage(new
Uri("/BananaTECH.jpg",UriKind.Relative));
|
Resource:
This Option will add the resource with in the application package
inside the Silverlight assembly .

On
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.
To
locate the resource there
should not be any leading trails
.
XAML :
?
Code :
?
| 1 |
image1.Source
= new BitmapImage(new Uri("BananaTECH.jpg",
UriKind.Relative));
|
None:This
option neither add the resource to the application package nor to the
Silverlight assembly.You need to add resource to the server manually.
With
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.
XAML
:
?
Code :
?
| 1 |
image1.Source
= new BitmapImage(new Uri("BananaTECH.jpg",
UriKind.Relative));
|
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 ,

Then
adding a reference to the Silverlight project will allow you to use the resource
file inside the application.

Resources
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.
?
Conclusion
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.
Source Code
Download sourcecode
ResourcesInSL