Silverlight image Path Problems

Source: Internet
Author: User
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 the similarities and differences between the two lines of code in XAML markup:

<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>

What is the backslash and no backslash? Is there any difference ?, 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?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 imageIn this example, the Resource 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 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.

 

Link: http://hi.baidu.com/liongg/blog/item/b91a495494e34152564e00b4.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.