Silverlight tips-path settings and resource files

Source: Internet
Author: User
Relative Path and absolute path. This article is from: http://www.cnblogs.com/suengcn/archive/2008/08/17/201993.html1,relative path and absolute path.
There are three types of document paths: absolute path, root relative path, and document relative path.

The absolute path is the full path that includes the Server Protocol (in this example, http). For example, the full path of "Hong Neng online-computer Park" is http://www.hongen.com/pc/index.htm. if you want to link a document outside the site, use absolute paths.

The relative path includes the Root relative path (Site Root) and Document relative path (Document.
As mentioned above, when editing a webpage on a local disk, You need to select a folder to define the local site and simulate the root folder on the server, the system determines the local file location of all links based on this folder, and the root in the relative path refers to this folder.

The path to the relative path (also known as the relative directory) starts with "/". The path is calculated from the root directory of the current site. For example, the myweb directory of the D disk we created earlier is the site named "myweb". Then, the path "/index.htm" indicates that the file location is D: \ myweb \ index.htm. The root relative path applies to files in the environment frequently when the link content is changed, so that the link can still take effect even if the files in the site are moved.

If the directory structure is too deep, it is better to use the relative path when referencing the files under the root directory. For example, if a webpage file references a graph in the img directory under the root directory, the relative path of the document in the current webpage is represented.

Note: When previewing a file, content linked with the root relative path is not displayed in the local browser, because the browser does not recognize the root folder of the site as a server.

In this case, we can virtualize the root directory of the site into a disk. For example, if you want to browse files under D: \ myweb, you can select "run" in the Windows "start" menu to bring up the "run" dialog box.

Enter "subst I: d: \ myweb". The myweb folder on disk d is virtualized as an I disk, and you can view the webpage under disk I in a browser, the content linked with the root relative path can be displayed normally.

The relative path of a document refers to the folder containing the current document, which is calculated based on the folder where the current webpage is located. For example, if the current webpage is located at D: \ myweb \ mypic, then:

A.htm indicates D: \ myweb \ mypic \ a.htm;

"../A.htm" is equivalent to D: \ myweb \ a.htm, where "../" indicates the level-1 folder on the current folder.

"Img/a.gif" refers to D: \ myweb \ mypic \ img \ a.gif. "img/" indicates the img folder under the current folder.

The relative path of a document is the simplest path. Generally, it is used to link documents stored in the same folder.

"/" In Silverlight

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

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>

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

Sivlerlight Resource Overview

Images, audios, videos, and even non-execution data files such as XML and XAML are called resources. The structure of the Silverlight application is different based on the resource location.

 

In. the xap compressed package contains an Appmanifest. the xaml file defines the Packaged assembly and application entry, one application assembly, zero or multiple library assembly, and zero or multiple resource files, slices, and videos. The whole. xap has been downloaded to the customer's local computer before the application is activated, and the on-demand file, as the name suggests, is loaded as needed.

 

 

It can be seen that resource files can be distributed in multiple locations, such as embedded in the Assembly; In the. xap compressed package, they are not embedded in any assembly; they are neither in the Assembly nor in. xap. Right-click a Resource such as pink.png image in the project and select properties. In "Build Action", select Resource, Content, and None to generate the Resource file in the preceding three locations. (The official document says that other options do not play a role in Silverlight, but in fact Embedded Resource also works. For details, refer to dynamic loading of XAML with reflection.)

 

 

Introduce resource files in the project, that is. in the same directory of xaml, the default "Build Action" option is "Resource", that is, it is embedded in the corresponding Assembly. In this case, referencing this Resource should not start with a backslash. If you put the resource file in the same directory as ClienBin, it is equivalent to None. The reference must start with a backslash. See Silverlight Image Source URI to see whether the difference starts with a backslash.

 

 

We do not recommend placing the resource file in the same directory of page. xaml, and then select "Build Action" as "None". In other words,If you do not want to package resource filesIn. xap, directly put the resource file under the ClienBin file directory.. However, you can set page. xaml to a resource file in the same directory.AttributeIn"Change Build Action to Content and place it in the. xap compressed package to facilitate the use of multiple. xap assemblies.Now that the resource file is in the. xap package, you must start with a backslash to reference the file. This is what is mentioned at the end of Silverlight Image Source URI. Why can resource files be referenced at the beginning of a backslash when they are stored in the same directory of page. xaml. The sample code and running result are as follows.

<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"/>
<Image x: Name = "pink" Source = "/images/pink.png" Stretch = "None"/>
</StackPanel>

Because the Build Action of pipeline is changed to Content, after being generated, it is located in the. xap compressed package (so it must be referenced at the beginning of the backslash) but not in any assembly.

 

 

 

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.