Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Most applications have files that are affiliated with the program but aren't source code or part of the runtime. These could be image files, sound effects, or sample documents. The key is that these are binary files that must be shipped with your application. In Silverlight, it's trivial to bundle these files with your assembly. It is also easy to extract these resources and use them in your application. In fact, some controls, like the image control, can extract the resource with just a URI location, no code necessary. For this demonstration, I am using Visual Studio and a project called UsingBundledResources.
Let's start by looking at my resources. They are over here in my Images folder. I have a resource file that I want to include with my application. This first JPG is marked as a resource file; you can see that down here in the Properties window, Build Action equal Resource. When you mark an item as a resource it is embedded in the dll file. This makes the dll larger. This second JPG is marked as Build Action content. When you mark an item as a content file, it is stored in the XAP file alongside the dll. The file is compressed inside the XAP and downloaded to the client.
Because the dll is smaller, the application start time is reduced. This third file represents a streamable resource. Note that this is a demo file that contains no content. The file is marked as Build Action None and Copy to Output Directory always. This file is not downloaded to the user's computer with the XAP file, but it should be copied to the companion web site. This is useful for video files and other streamable content. There is no compression benefit from adding a video file to the XAP. Instead, it can be streamed to the consumer computer from the web server as needed.
You do need to copy the streamable file to your companion web site. To use these two JPEGs, I am going to use an image control on this user control. Here's my XAML. Here's my image. Here is my source. I am specifying a source as the Image folder and that content file. For this image control, I am doing exactly the same. From the perspective of my XAML, this is quite easy. The only difference between these two is whether or not you have to have a leading slash in this resource. In the resource, you do not have a leading slash. To load the resource into my code behind, I am going to go to this button and show you how you might load a binary resource.
On this button click procedure, the first thing I do is go to the Application class and ask for a resource that's stored inside the assembly. Remember, this one was marked as a resource? So I tell it to get the resource as a stream. The stream is just an array bytes. I have given it a Uri that represents the location. The important part here is the name of my assembly and then the path to the resource, in this case Images/coast_resource.jpg.
Once I have the streamInfo, I can grab the content out of it by using streamInfo.Stream. I am going to put that inside a brand-new BitmapImage, set its source and then I am going to go to my image control on the page and say its source is equal to this BitmapImage. The other approach doing with this image is to use this syntax. Declare a variable as a new bitmap image, and the bitmap image itself knows how to read a Uri and create the stream. So I am using that approach up here with my content file.
Same code, just using a shorter syntax which works with files marked as content. So I am using that syntax up here in the LoadContentbutton. You can instantiate a bitmap image and use one of these constructors which can read the content from a Uri. Much simpler syntax. Let's see if this application works. I'll press F5, click on this Load resource. It quickly loads the resource. Click on the content and we have success. In this demo, I used a byte array to retrieve the information, which is a low- level way of doing business.
That works for other resources like sound effects. You will see more about those types of resources later.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100273 Viewers
56 Video lessons · 113240 Viewers
71 Video lessons · 82124 Viewers
131 Video lessons · 39411 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.