Silverlight3 Study Notes (2): Silverlight initial contact

Source: Internet
Author: User
Tags mscorlib microsoft website
When creating a Silverlight project, you will be prompted whether to create a bearer project, as shown in. NETMVC2 Web project, so there are three types of Web projects that carry Silverlight: ASP. NETWeb application project, ASP. NET website and ASP. NETMVCWeb

When creating a Silverlight project, you will be prompted whether to create a bearer project, as shown in. NET MVC2 Web project, so there are three types of Web projects that carry Silverlight: ASP. NET Web application project, ASP. NET website and ASP. net mvc Web


Create a Silverlight Project
When creating a Silverlight project, we will prompt whether to create a bearer project, as shown in:

In VS2010, you can directly create an ASP. NET MVC2 Web project, so there are three types of Web projects that carry Silverlight: ASP. NET Web application project, ASP. NET website and ASP.. net mvc Web application project. The ASP. NET Web application project can provide the same Web project semantics as the Visual Studio. NET 2003 Web project. Its compilation model is similar to the Visual Studio. NET 2003 compilation model. All code files (independent files, hidden files, and class files) in the project will be compiled into a single assembly and stored in the Bin directory. Since compilation creates a single assembly, you can specify attributes such as the Assembly name and version. If we only develop Silverlight, you can select one type at will to view the Running Effect of Silverlight on the webpage.
VS2010 Interface
After successfully creating the Silverlight project, you can see the following interface:


In VS2010, you can drag controls directly from the toolbox to the Silverlight interface. In the development of ASP. NET, a page is divided into two parts. aspx. cs and. cs, the former contains the design code, the latter contains the business logic code, and ASP.. NET development, each control or page in a Silverlight project is also divided. xaml and. xaml. cs ,. the xaml Code also contains the design code ,. xaml. cs contains the business logic code. By default, each Silverlight project has an app. the xaml file, which is somewhat similar to the program in the WinForm project. cs class, including the settings when the project is started. After a Silverlight project is created successfully, the app. xam. the cs code is as follows:

Using System; using System. collections. generic; using System. linq; using System. net; using System. windows; using System. windows. controls; using System. windows. documents; using System. windows. input; using System. windows. media; using System. windows. media. animation; using System. windows. shapes; namespace SilverlightDemo1 {public partial class App: Application {public App () {this. startup + = this. application_St Artup; this. exit + = this. application_Exit; this. unhandledException + = this. application_UnhandledException; InitializeComponent ();} private void Application_Startup (object sender, StartupEventArgs e) {this. rootVisual = new MainPage ();} private void Application_Exit (object sender, EventArgs e) {} private void Application_UnhandledException (object sender, ApplicationUnhandledExceptionEventArgs e) {// If the application runs outside the debugger, report the exception using the browser's // exception mechanism. On IE, A // yellow alarm icon will be used in the status bar to display this exception, while Firefox will display a script error. If (! System. Diagnostics. Debugger. IsAttached) {// Note: This allows the application to continue running when an exception has been thrown but has not been handled. // For production applications, this error handling should be replaced by reporting errors to the website // and stopping the application. E. handled = true; Deployment. current. dispatcher. beginInvoke (delegate {ReportErrorToDOM (e) ;}} private void ReportErrorToDOM (ApplicationUnhandledExceptionEventArgs e) {try {string errorMsg = e. predictionobject. message + e. predictionobject. stackTrace; errorMsg = errorMsg. replace ('"','/''). replace ("/r/n", @ "/n"); System. windows. browser. htmlPage. window. eval ("throw new Error (/" Unhandled Error in Silverlight Application "+ errorMsg +"/");") ;}catch (Exception ){}}}}
You can set the RootVisual attribute in the Application_Startup () method to specify the page to be started as the Silverlight startup interface.
About XAML
Using XAML in Silverlight as the interface design language of Silverlight, XAML is a special XML format (just like XHTML is also a special XML format ), now we don't need to pay too much attention to it. After further exercises, we will gradually understand the features of the XAML language. The following is a common XAML Page code:


Compilation of the Silverlight Project
Compile. Csc.exe is also used to compile vscompilation projects. Unlike the previous compilation of. NET projects, a command line parameter is added to the compilation of Silverlight projects. The command line parameter is "nostdlib". Its function is described as follows:

We can see that the function of using the "nostdlib" parameter is not to reference the standard library. In. the core class library in the. NET class library is mscorlib. dll. The function of using this parameter is not to reference mscorlib. dll (because Silverlight must support multiple browsers across operating systems, it cannot have the characteristics of Windows ). Although C # Or VB. NET programming can be used in Silverlight, the subset of the. NET class library used in Silverlight is limited in some usage. Even some classes or some methods appear in the runtime environment of Silverlight, but these classes and these methods cannot be used by developers, fortunately, these classes or methods will not appear in the intelligent perception that occurs during code writing for unnecessary troubles.
After the Silverlight project is compiled successfully, a dll file is generated. Its file structure is as follows:

In addition to a dll file, the following file is generated:
A. pdb file. Assume that the Silverlight project name is SilverlightDemo1 and the pdb file name is SilverlightDemo1.pdb. This file contains the debugging information required by.
A file named AppManifest. xaml. Regardless of the Silverlight project name, the file name will never change. The file contains the Assembly required by the current Silverlight project. The content of AppManifest. xaml in this instance is as follows:

An html file for testing. Assume that the silverlightproject name is silverlightdemo1,the htmlfile name is silverlightdemo1testpage.html, that is, the silverlightproject name is }testpage.html, that is, the html file with the Silverlight project name and TestPage as the file name. This file demonstrates how to embed the Silverlight control in the page. Its code is as follows:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <! -- Saved from url = (0014) about: internet --> <pead> <title> SilverlightDemo1 </title> <mce: style type = "text/css"> <! -- Html, body {height: 100%; overflow: auto;} body {padding: 0; margin: 0 ;}# silverlightControlHost {height: 100%; text-align: center ;} --> </mce: style> <style type = "text/css" mce_bogus = "1"> html, body {height: 100%; overflow: auto;} body {padding: 0; margin: 0 ;}# silverlightControlHost {height: 100%; text-align: center ;}</style> <mce: script type = "text/javascript"> <! -- Function onSilverlightError (sender, args) {var appSource = ""; if (sender! = Null & sender! = 0) {appSource = sender. getHost (). source;} var errorType = args. errorType; var iErrorCode = args. errorCode; if (errorType = "ImageError" | errorType = "MediaError") {return ;} var errMsg = "unhandled errors in the application" + appSource + "/n"; errMsg + = "Code:" + iErrorCode + "/n"; errMsg + = "category: "+ errorType +"/n "; errMsg + =" message: "+ args. errorMessage + "/n"; if (errorType = "ParserError") {errMsg + = "file: "+ Args. xamlFile + "/n"; errMsg + = "row:" + args. lineNumber + "/n"; errMsg + = "Location:" + args. charPosition + "/n";} else if (errorType = "RuntimeError") {if (args. lineNumber! = 0) {errMsg + = "row:" + args. lineNumber + "/n"; errMsg + = "Location:" + args. charPosition + "/n";} errMsg + = "method name:" + args. methodName + "/n";} throw new Error (errMsg);} // --> </mce: script> </pead> <body> </ptml>
As you can see in the Code, if the client browsing this page does not install the Silverlight client, you will see a prompt to download and install the Silverlight client, click the link to download Siverlight from the official Microsoft website. After the download is complete, the system prompts you whether to install the tool. That is, when you download and install Silverlight, you will receive a clear prompt that you can choose whether to download or install Silverlight. This is worth learning from Chinese companies. Software provided by many companies in China that are "legitimate" and "secure" are always vague and confusing some concepts to achieve their own ulterior motives, there are even hundreds of millions of installed software that often scans users' non-system disks outside of sensitive areas. These companies should take a good look at foreign software practices.
Some dll files. These dll files are the dependent files required to run the current Silverlight project. In the AppManifest. xaml file, we can see the following files except SilverlightDemo1.dll:
System. ComponentModel. DataAnnotations. dll
System. Windows. Controls. Data. dll
System. Windows. Controls. Data. Input. dll
System. Windows. Controls. dll
System. Windows. Controls. Navigation. dll
System. Windows. Data. dll
Zh-Hans/System. ComponentModel. DataAnnotations. resources. dll
Zh-Hans/System. Windows. Controls. resources. dll
Zh-Hans/System. Windows. Controls. Data. resources. dll
Zh-Hans/System. Windows. Controls. Data. Input. resources. dll
Zh-Hans/System. Windows. Controls. Navigation. resources. dll
Zh-Hans/System. Windows. Data. resources. dll
The above files are dependency files that run the Siverlight project. Among them, files in the zh-Hans folder are some resource files. Of course, in addition to files in zh-Hans, there are other folders similar to the zh-Hans folder, the file names in these files are the same as those in the zh-Hans folder. These files are language resource files.
Speaking of the dependency files of the Silverlight project, you have to talk about the core files of the Silverlight project. We know that the Siverlight program is downloaded to the client for execution. Therefore, you need to install the Silverlight runtime environment on the client. To facilitate users in different network environments to download and install the Silverlight runtime environment, therefore, this runtime environment should be as streamlined as possible (in fact only 5 MB). Since it is streamlined, it is naturally impossible to provide.. NET Framework. The following assembly is included in the Silverlight runtime environment:
Mscorlib. dll: Pay attention to this mscorlib. dll non. mscorlib. dll, but in the Silverlight runtime environment, it provides mscorlib similar to the. NET Framework. dll is the core class library (CORE) in the Silverlight runtime environment ).
System. dll: provides generic, URI processing, and regular expression-related functions.
System. Core. dll: provides support for LINQ.
System. Net. dll: Provides network functions, so that we can download Web files and create a Socket-based network connection.
System. Window. dll: Provides the class for creating the UI in Silverlight.
System. Window. Browser. dll: provides classes for interactive operations on HTML elements.
System. Xml. dll: Provides XmlReader and XmlWriter classes to process Xml.
The dll except SilverlightDemo1.dll that appears in AppManifest. xaml is not in the core class library provided by the Silverlight runtime environment. As mentioned above, the design concept of the Siverlight runtime environment is to make it installation and download as small as possible, so the designers of the Siverlight runtime environment provide some classes outside the core class library, the functions provided by these classes are not used in every Silverlight application (if they are used, they will be placed in the core class library). Therefore, they are provided in the form of additional class libraries. When VS is used to compile a Silverlight project, VS automatically detects the dll that is required by the project but not in the Silverlight core class library, so we will see some dll files in the bin directory that we don't know how to run.
The following dll is often used in these additional class libraries: System. window. controls. dll, System. window. controls. data. dll, System. window. controls. data. input. dll, System. window. controls. input. dll and System. windows. controls. navigation. dll. Indeed, these files are stored in our AppManifest. in the xaml file.
A. xap file. And then decompress the software ). Use WinRAR to directly open SilverlightDemo1.xap:

We can see the AppManifest mentioned above. the xaml and SilverlightDemo1.dll files, as well as the "some" dll files improved above, all of these dll files are in AppManifest. additional dll required for the SilverlightDemo1 project that appears in xaml. The xap method has two advantages: xap files are compressed files, which can reduce network traffic and increase download speed; and xap is easy to deploy, if you want to deploy Silverlight to another website, you just need to copy the xap file to the directory where the website is located and create a page embedded with Silverlight, which is super simple.
Deployment of the Silverlight Project
As mentioned above, deploying a Silverlight project requires only an xap file and a webpage file containing how to set the Silverlight project. Sometimes, when a project is created, a name is named, and the name is not intuitive at the end of the release, we can use VS to set the final generated Silverlight project file name. The specific method is to right-click the Silverlight project and choose "properties". The following window is displayed:

As you can see, in this panel, we can specify the following things related to the Silverlight project:
Application Assembly name: The final dll file name;
Default space name: The namespace name used by default when the code file is created;
Startup Object Name: You can see why in App. the Application_Startup () method in xaml specifies the form to be started, because the App object is started by default, so its formulation is useful, if you create multiple apps in the project. multiple options will appear here for The xaml file;
Version of the generated Silverlight application: if multiple Silverlight sdks are installed, multiple options are available;
Generate the xap file name: You can change it to a more intuitive name.

From the previous sections, we can see that Silverlight will eventually be downloaded to the client for running. The client downloads an xap file, which is essentially a compressed file. If you decompress this file, you can obtain the dll file of the Silverlight project. If you are interested, you can use some decompilation tools to obtain the source code, for example, use the famous decompilation software Reflector to open the dll file in this article:

We can see that the software can be used to conveniently view the data that has not been processed. NET assembly or Silverlight assembly code (stored in the form of MSIL), so for security, do not store any private, security-related data in Silverlight. In addition, you can encrypt or confuse the generated assembly. Encryption means that the decompilation tool cannot decompile the source code by leveraging some features of the MSIL command, obfuscation is to change the variable name and method name in the Assembly, so that it does not have the characteristics of understanding, so that even if the source code is decompiled, it is hard to read. In VS, a Community version of the code obfuscation tool Dotfuscator is provided. This is a free. NET code obfuscation tool with limited functions. Its interface is as follows:

For general projects, you can use Dotfuscator for obfuscation, which can make general visitors daunting.
Code Protection for software is a topic that has been being discussed for a long time. In this article, we will only make everyone aware of this, instead of letting everyone focus most of their efforts on how to organize and snoop their source code. The anti-protection and protection of software source code has always been a problem of spear and shield. The two are competing with each other to avoid this problem in any language.
Summary

2010-09-13

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.