Currently, silverlight2 supports JPG and PNG formats, and some PNG codes are not supported. At the same time, some PNG files are invisible in the design preview of XAML, but are visible at runtime. See two lines in the XAML markup. Code Similarities and Differences:
<Stackpanel background = "white" orientation = "vertical">
<Image X: Name = "blue" Source = "/images/blue.png" stretch = "NONE"/>
<Image X: Name = "green" Source = "images/green.png" stretch = "NONE"/>
</Stackpanel>
Backslash forward-slash? Is there a difference between Xiami ?, Named resource test example solution Resource Manager and running effect: Blue images starting with a backslash must be placed in the corresponding folder in the clientbin directory; green images that do not begin with a backslash should be placed in the corresponding folder under the Resource Directory. Otherwise, an imageerror occurs.
Why? The two are both relative paths. What is the difference between the backslash? The relative path at the beginning of backslash , which represents the root directory where the application Program runs, that is, . in the xap compressed file, If the file to be referenced cannot be found here, the fallback mechanism of the relative path is automatically in . the directory where xap is located . In this example, It is referenced in the clientbin directory. If there are no two locations, an error will occur. not starting with "/" indicates the directory where the XAML file that references the image is located , in this example, page. resource directory where the 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 green image, but there is no blue image. In fact, it is obvious that the images folder where blue is located is similar to. xap, which is naturally not included. We can see that green images starting with a backslash are not embedded in the xap file in the Silverlight program and downloaded directly to the client, while Blue images are 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 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
In other words, if we use urifiles such as http://www.liongg.net/test.jpg, we will not be able to avoid this. In the code, you can also use methods such as application. Current. Host. Source. absolutepath. I tried it and found that it was too troublesome and I didn't want to worry about multiple methods.
A little deeper, even if the image is placed in a folder at the same level as page. XAML, you can use a backslash to reference it. You only need to select build action as "content" in the corresponding image attribute, but the image is still put. the xap package is in. For more information, see sivlerlight resource overview.
Reprinted: http://hi.baidu.com/liongg/blog/item/b91a495494e34152564e00b4.html