Include Files in Application Package in Silverlight 2.0


In my previos article i dicussed about how to Include Files as Resources and load them in Silverlight 2.0.

Files can be included in the application .xap package. A .xap file is simply a zip file containing the compiled output files. You can rename the .xap extension to .zip, and then unzip the contents. To include a file in application package, simply set the 'Build Action' to 'Content' and rebuild the solution.

To load the files from the application package use the following code:

using System.Windows.Resources;
using System.Windows.Media.Imaging;
...

StreamResourceInfo
sr2 = Application.GetResourceStream(

                new Uri("blue.png", UriKind.Relative));

            BitmapImage bmp2 = new BitmapImage();

            bmp2.SetSource(sr2.Stream);

            Image2.Source = bmp2;

Note: The URI is now URI just "blue.png". If the images were stored in a subfolder 'Images', the URI would be "Images/blue.png".

Limitation

If Application.GetResourceStream() returns nothing or null, it may be due to: Visual Studio 2008 prevents the compiler from rebuilding the resources. The work around is to go to the project folder and delete all the *.resources files in the subfolders or just delete the 'Obj' folder and press F5 to run the solution.

Up Next
    Ebook Download
    View all
    Learn
    View all
    F11Research & Development, LLC