WPF and Silverlight program coding material collection

Source: Internet
Author: User
Tags reflector

1. Obtain path information

1. System. Diagnostics. process. getcurrentprocess (). mainmodule. filename
Obtain the complete path of the module.
2. System. environment. currentdirectory
Obtain and set the fully qualified directory of the current directory from which the process starts.
3. System. Io. Directory. getcurrentdirectory ()
Get Application Program . This is not necessarily the directory from which the program starts. It may be stored in c: \ www. This function may return c: \ Documents and Settings \ zyb \\, or C: \ Program Files \ Adobe \. Sometimes it doesn't necessarily return anything, and I don't understand it.
4. System. appdomain. currentdomain. basedirectory
Obtain the base Directory of the program.
5. System. appdomain. currentdomain. setupinformation. applicationbase
Obtain and set the name of the directory containing the application.
6. System. Windows. Forms. application. startuppath
Obtain the path of the executable file that started the application. The effect is the same as that of 2 and 5. Only five strings are followed by one more "\".
7. System. Windows. Forms. application. executablepath
Obtain the path and file name of the executable file that started the application. The effect is the same as that of 1.

8. In WPF, mycollection ["system_curdirectory"] = application. startuppath; cannot be used to obtain future information.

 For the sequential path, use: mycollection ["system_curdirectory"] = system. Io. Path. getdirectoryname (process. getcurrentprocess (). mainmodule. filename );

 

2. Obtain the path URI in the Silverlight. Web project

Uri is a simple representation of resources that can be used by applications on the Intranet or the Internet.UriClass defines attributes and methods to process Uris, including analysis, comparison, and combination.

UriThe class attribute is read-only. To create an object that can be modified, useUribuilderClass.

Relative uri (for example, "/New/index.htm") must be expanded relative to the base Uri, which is absolute. ProvidedMakerelativeuriTo convert an absolute URI to a relative URI if necessary.

If the URI string is a well-formatted Uri and contains a scheme identifier,UriThe constructor will not escape the URI string.

UriThe attribute returns the canonicalized data representation using escape encoding. Any character with a unicode value greater than 127 will be replaced with an equivalent hexadecimal number. To make the URI have a canonicalized format,UriThe constructor performs the following steps:

    • Convert the URI scheme to lowercase.

    • Convert the host name to lowercase.

    • If the host name is an IPv6 address, the normalized IPv6 address is used. Scopeid and other optional IPv6 data will be removed.

    • Remove the default and empty port numbers.

    • Normalize the hierarchical URI path by compressing sequences such as //, //, and // (including escape representation. Note that in some scenarios, escape representation is not compressed.

    • For a layered Uri, if the host does not end with a forward slash (/), add a forward slash.

    • Any reserved characters in the URI must be escaped according to RFC 3986.

In some constructor schemes, as part of the normalization work, the escape representation is compressed. Its URI compresses escape sequences by file, HTTP, https, net. pipe, and net. TCP. All other solutions do not compress escape sequences. For example, if you encode two vertices ".." as "% 2e % 2e" in percent form, the URI constructor compresses this sequence for some schemes. For exampleCodeThe example demonstrates the URI constructor of the HTTP scheme.

Code

Method 1:

// Gets the uri of the subscribed XAML file of the specified XAML content to be rendered.
VaR strfullurl = Application. Current. Host. Source. absoluteuri;
If (Strfullurl. indexof ( " Clientbin " ) > 0 )
{
VaR uristr = Strfullurl. substring ( 0 , Strfullurl. indexof ( " Clientbin " )) + " Report/default. aspx " ;
VaR URI = New Uri (uristr );
}

Method 2:

VaR URI = New Uri (App. Current. Host. source, " ../Report/default. aspx " );

Method 3:

String URL = System. Windows. browser. htmlpage. Document. documenturi. tostring ();
VaR struri = URL. substring ( 0 , URL. lastindexof ( " / " )) + " /Report/default. aspx " ;
VaR URI = New Uri (struri );

Method 4:

Get the webpage root directory

VaR Xapuri = Application. Current. Host. source;
VaR URI = New Uri (Xapuri, " ../ " );

 

Iii. various situations in which the URI in Silverlight references resource files

This section describes how to reference different build actions in the resource files (images, videos, fonts, etc.) in Silverlight In The XAML or C # background.
Take the image file as an example to introduce various build actions. (After selecting a file, press F4 to open the vs attribute panel, you can see the build action option and change it)
Assume that we have two image files named silverlight.pngand sl.jpg, add silverlight.png to the root directory of the application, and put the mainpage. XAML file in a folder, and sl.jpg in a new images folder.
1. Resource:
After this method is selected, the resource file will be embedded in the application set, that is, the generated xap cannot be opened.
It can be accessed with a relative URI relative to the current XAML file, such as <Image Source = "silverlight.png"/> or <Image Source = ". /silverlight.png "/>, you can use <Image Source = ". /images/sl.jpg "/> access
The safest way is to use the unique Assembly Resource URI for access. The format is
<Image Source = "/{assemblyshortname}; component/foo.jpg"/>This method can also reference images in other xap assembly
Modify the reference method of the two images

Code:

<Image Source = "/silverlightapplication1; component/silverlight.png"/>
<Image Source = "/silverlightapplication1; component/images/sl.jpg" Height = "100"/>

You can see that the same can be displayed normally.
If no file is found, the system will not find another location and the imagefailed event will be triggered.
When build action is resource, you can use several relative URIs to reference resource files.

Code:

<Image Source = "/silverlightapplication1; component/silverlight.png" Height = "100"/>
<Image Source = "./Silverlight. PNG" Height = "100"/>
<Image Source = "Silverlight. PNG" Height = "100"/>
<Image Source = "/silverlightapplication1; component/images/sl.jpg" Height = "100"/>
<Image Source = "./images/sl.jpg" Height = "100"/>

Note that system resources generated in this mode can be directly obtained using application. getresourcestream (URI). stream in the code.

2. content:
The "root" directory of the application, and the sl.jpg file is stored in the images folder.
In this case, the relative URI of the two images must start "/".

Code:

<Image Source = "/Silverlight. PNG" Height = "100"/>
<Image Source = "/images/sl.jpg" Height = "100"/>

Note: in this way, if the image file is not found in xap, Silverlight will automatically find the required image file from the folder where the current xap application is located, if not found, the imagefailed event is triggered. This method is applicable when multiple assemblies reference the same file.
3. None:
None indicates that the image file is not generated and will not be copied to the xap file. You must consider the "copytooutputdirectory" option, this option has three options: "Do not copy", "copy always", "copy If newer", because we need to reference image files, images files, and an images folder, the sl.jpg file is in the images folder.
The URI method of the referenced file is the same as the second content method.
In most cases, we want to put the video/audio file out of the xap, because this file is generally large and will affect the loading of the Silverlight application, in addition, generally, video and audio files are compressed and stored in xap does not reduce their file size.
Resource file generation operations such as image and video are the same as those when they are not added to the project. They can be referenced using absolute Uri.
Summary:
Relative URI reference: [/I]

Code:

<Image Source = "/Silverlight. PNG" Height = "100"/>
<Image Source = "/images/sl.jpg" Height = "100"/>

[I] absolute path reference:

 

Code:

<Image Source = "http: // localhost: 50235/clientbin/Silverlight. PNG" Height = "100"/>
<Image Source = "http: // localhost: 50235/clientbin/images/sl.jpg" Height = "100"/>

4. embeddedresource:
This method will embed the file into the assembly, and Silverlight cannot reference this file in XAML and C # Through Uri, microsoft does not recommend that you use this method in Silverlight to embed resources in a collection.
You can use
Reflection. Assembly. getexecutingassembly (). getmanifestresourcestream (string path)Related methods to get stream reference of the file
I suggest you change it to the first "resource" method, and then use the first reference method.
I will not introduce the differences between the resource method and embeddedresource. If you are interested, you can use the reflector tool to open your Assembly DLL file. The differences between the two can be seen in the resources directory, resources embedded with resource will be placed in appname by the system. g. resources, and resources using the embeddedresource method will be directly put under the Resources Directory, other details will not be introduced.

 Page:
All user controls, pages, and child forms (usercontrol/page/childwindow) should be generated using the operation, otherwise, the code file corresponding to the background cannot be linked to this XAML file. Errors in the XAML when "page" build action is used will cause the project to fail to be correctly generated.
Compile
The class file must be generated using "compile", that is, the. CS or. VB file in the project.
Applicationdefinition
The Silverlight program's entry XAML file (App. XAML by default) should be set to this "application definition"
Entity deploy
This is the generation method used by entityframework. It is useless in Silverlight.
Splashscreen
"Splashscreen" is used for the WPF startup screen. The Silverlight boot screen is implemented in other ways. Therefore, do not use this method in Silverlight.

 

 

This section describes how to reference different build actions in the resource files (images, videos, fonts, etc.) in Silverlight In The XAML or C # background.
Take the image file as an example to introduce various build actions. (After selecting a file, press F4 to open the vs attribute panel, you can see the build action option and change it)
Assume that we have two image files named silverlight.pngand sl.jpg, add silverlight.png to the root directory of the application, and put the mainpage. XAML file in a folder, and sl.jpg in a new images folder.
1. Resource:
After this method is selected, the resource file will be embedded in the application set, that is, the generated xap cannot be opened.
It can be accessed with a relative URI relative to the current XAML file, such as <Image Source = "silverlight.png"/> or <Image Source = ". /silverlight.png "/>, you can use <Image Source = ". /images/sl.jpg "/> access
The safest way is to use the unique Assembly Resource URI for access. The format is
<Image Source = "/{assemblyshortname}; component/foo.jpg"/>This method can also reference images in other xap assembly
Modify the reference method of the two images

Code:

<Image Source = "/silverlightapplication1; component/silverlight.png"/>
<Image Source = "/silverlightapplication1; component/images/sl.jpg" Height = "100"/>

You can see that the same can be displayed normally.
If no file is found, the system will not find another location and the imagefailed event will be triggered.
When build action is resource, you can use several relative URIs to reference resource files.

Code:

<Image Source = "/silverlightapplication1; component/silverlight.png" Height = "100"/>
<Image Source = "./Silverlight. PNG" Height = "100"/>
<Image Source = "Silverlight. PNG" Height = "100"/>
<Image Source = "/silverlightapplication1; component/images/sl.jpg" Height = "100"/>
<Image Source = "./images/sl.jpg" Height = "100"/>

Note that system resources generated in this mode can be directly obtained using application. getresourcestream (URI). stream in the code.

2. content:
The "root" directory of the application, and the sl.jpg file is stored in the images folder.
In this case, the relative URI of the two images must start "/".

Code:

<Image Source = "/Silverlight. PNG" Height = "100"/>
<Image Source = "/images/sl.jpg" Height = "100"/>

Note: in this way, if the image file is not found in xap, Silverlight will automatically find the required image file from the folder where the current xap application is located, if not found, the imagefailed event is triggered. This method is applicable when multiple assemblies reference the same file.
3. None:
None indicates that the image file is not generated and will not be copied to the xap file. You must consider the "copytooutputdirectory" option, this option has three options: "Do not copy", "copy always", "copy If newer", because we need to reference image files, images files, and an images folder, the sl.jpg file is in the images folder.
The URI method of the referenced file is the same as the second content method.
In most cases, we want to put the video/audio file out of the xap, because this file is generally large and will affect the loading of the Silverlight application, in addition, generally, video and audio files are compressed and stored in xap does not reduce their file size.
Resource file generation operations such as image and video are the same as those when they are not added to the project. They can be referenced using absolute Uri.
Summary:
Relative URI reference: [/I]

Code:

<Image Source = "/Silverlight. PNG" Height = "100"/>
<Image Source = "/images/sl.jpg" Height = "100"/>

[I] absolute path reference:

 

Code:

<Image Source = "http: // localhost: 50235/clientbin/Silverlight. PNG" Height = "100"/>
<Image Source = "http: // localhost: 50235/clientbin/images/sl.jpg" Height = "100"/>

4. embeddedresource:
This method will embed the file into the assembly, and Silverlight cannot reference this file in XAML and C # Through Uri, microsoft does not recommend that you use this method in Silverlight to embed resources in a collection.
You can use
Reflection. Assembly. getexecutingassembly (). getmanifestresourcestream (string path)Related methods to get stream reference of the file
I suggest you change it to the first "resource" method, and then use the first reference method.
I will not introduce the differences between the resource method and embeddedresource. If you are interested, you can use the reflector tool to open your Assembly DLL file. The differences between the two can be seen in the resources directory, resources embedded with resource will be placed in appname by the system. g. resources, and resources using the embeddedresource method will be directly put under the Resources Directory, other details will not be introduced.

page:
all user controls, pages, and child forms (usercontrol/page/childwindow) should be generated, otherwise, the code file corresponding to the background cannot be linked to this XAML file. Errors in the XAML when "page" build action is used will cause the project to fail to be correctly generated.
compile
class files must be generated using "compile", which is the project. CS or. VB file
applicationdefinition
Silverlight program entry XAML file (APP by default. XAML) It should be set to this "application definition"
entity deploy
This is the generation used by entityframework. In Silverlight,
splashscreen
"splashscreen" is used by the WPF startup screen, other methods are used to start the loading screen of Silverlight. Therefore, do not use this method in Silverlight.

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.