Labels

Monday, August 30, 2010

Including Binary Resources in Silverlight

Resources (Image) can be added in three different ways:

 

 

 

 

In the Application Assembly (DLL)

·         Build Action – Content

·         Copy to Output Directory – Do not Copy

 

See Code 1 & 2.

In the Application Package (XAP)

·         Build Action – Resource

·         Copy to Output Directory – Do not Copy

 

See Code 3.

In the Silverlight Test Web Site Project

·         Must be placed in ClientBin Folder

 

See Code 4

In Separate SL Class Library Assemblies

·         Build Action – Resource

·         Copy to Output Directory – Do not Copy

 

See Code 5

 

 

Code

 

1

<Grid x:Name="LayoutRoot" Background="White">

            <Image x:Name="img" Source="Image_Resource.jpg"></Image>

</Grid>

 

2

<Grid x:Name="LayoutRoot" Background="White">

            <Image x:Name="img"></Image>

</Grid>

 

public Constructor()

{

            InitializeComponent();

            StreamResourceInfo sri = Application.GetResourceStream(new Uri("Resources;component/Image_Resource.jpg",

                                                UriKind.Relative));

           

            BitmapImage bitmap = new BitmapImage();

            bitmap.SetSource(sri.Stream);

            img.Source = bitmap;                

}

 

3

<Grid x:Name="LayoutRoot" Background="White">

            <Image x:Name="img" Source="/Image_Content.jpg"></Image>

</Grid>

 

4

<Grid x:Name="LayoutRoot" Background="White">

            <Image x:Name="img" Source="/Image_Webfile.jpg"></Image>

</Grid>

 

5

<Grid x:Name="LayoutRoot" Background="White">

            <Image Source="/ResourceClassLibrary;component/happyface.jpg"></Image>

</Grid>

 

Hope this helps.

 

Thanks & Regards,

Arun Manglick

 

 

 

 

 

 

 

 

 

 

No comments:

Post a Comment