Adding images to Silverlight currently supports JPG and PNG formats. How can I add images to a directory?
<Stackpanel background = "white" orientation = "vertical"> <br/> <image X: name = "AA" Source = "/images/a.png" stretch = "NONE"/> <br/> <image X: name = "BB" Source = "images/B .png" stretch = "NONE"/> <br/> </stackpanel> <br/>
Backslash forward-slash? Is there any difference? The image a starting with a backslash needs to be placed in the corresponding folder under the clientbin directory of the website. The B image not starting with a backslash should be placed in the corresponding folder under the Silverlight directory. Otherwise, an imageerror occurs.
Start with a backslashIs the root directory of the application program, that isIn the. xap compressed package,If the file to be referenced is not found, the reverse mechanism of the relative path (fallback mechanism) is automaticallyIn the directory where. xap is locatedIn this example, the clientbin directory is used for reference. If there are no two locations, an error will occur.Not starting "/", The relative position isDirectory of The XAML file that references the imageSuch as the directory where the page. XAML file is located.
So which method is used? Rename the xap file as a zip file, decompress it, and decompile the DLL file with reflector. It is found that it contains the B image, but there is no a image. In fact, it is also obvious that the images folder where A is located is similar to. xap, which is naturally not included. As a result, we can see that image a starting with a backslash is not embedded in the xap file of the Silverlight program and downloaded directly to the client, while image a is requested as needed (on-demand ), download the SDK when it is displayed. When the data volume is large, it takes too long to load the program without starting with a backslash, and the user experience is poor. This scheme is naturally unavailable if it starts, however, if it starts with a backslash, it is not visible in the design preview of XAML. It can be seen only when the program is running. (For preview, copy a copy from clienbin and put it in the same directory of page. XAML for design and use. Delete the copy when the program is released .) In fact, you can test it in testpage. aspx to see the image, but it cannot be displayed in page. XAML.
In addition to directly determining the source URI of the image in XAML, you can also determine it in code-behind. At this time, the backslash usage is consistent with that in XAML.
C #: Image IMG = new image ();
IMG. Source = new bitmapimage (New uri ("test.jpg", urikind. Relative); // under the directory of page. XAML
// IMG. Source = new bitmapimage (New uri ("/test.jpg", urikind. Relative); // under the directory where. xap is located
For example, if you use a class such as http://www.csdn.net/test.jpg, you will not be able to use uridirectly. You can also use methods such as application. Current. Host. Source. absolutepath in the code.
Further down, the image is placed in a folder at the same level as page. XAML, and can be referenced using a backslash. You only need to set the build action in the properties of the corresponding image.
Select "content", but the image is still put in the. xap compressed package. This is worth noting.