Silverlight classic 10 questions for you

Source: Internet
Author: User
Tags hosting

Reprinted from http://tech.it168.com/a2009/0518/362/000000362992.shtml

This article is helpful for getting started with Silverlight.

[It168] This article answers the 10 most common questions about Silverlight. In a sense, these two technologies are interrelated: they are all about the interface performance technology. Furthermore, Silverlight is based on WPF and is a subset of it. This article not only introduces these two technologies theoretically, but also provides some small examples for your reference.
Part 1:FAQs about WPF classic 9
1st Q: What is a dependency attribute?

Dependency attributes have a notable feature: dependency attributes belong to a class, but can be used in another class. Let's take a look at the followingCode:

< Rectangle Height = "72" Width = "131" Canvas. Left = "74" Canvas. Top = "77" />

Height and width are common attributes of the cube object. However, the top (canvas. Top) and left (canvas. Left) are dependency attributes. Because they all belong to the canvas class. It is used to specify the position of a rectangle in the canvas.

2nd Q: Will the XAML file be compiled or constructed at runtime?

Generally, a XAML file is applied in the XAMLProgramCompiled before running. However, it also supports compilation at runtime. When creating a XAML-based project, you will see the file g created by Visual Studio in the OBJ \ debug folder of the project. for each XAML file, you will find a G. CS file. For example, if there is a shiv. XAML file in our project, you will find the shiv. G. CS file in the OBJ \ debug folder. Simply put, you will not see the XAML file at runtime. However, if you want to complete loading and parsing of the XAML file at runtime, you can also.

3rd Q: How to separate program code and XAML?

Separating program code and XAML is one of the most important features of WPF. In this way, the UI Designer of the software can focus on the interface performance of the software independently, while the programmer can focus on the Code logic of the software without worrying about the performance of the software.

Figure 1 separation of program code and XAML

The code snippet above demonstrates how the XAML file is separated from the logic code behind it and how it is associated. To associate a XAML file with a class, we need to specify the class attribute (X: Class) of The XAML ). By defining the event sender and event value of a method, the event of a XAML object can be connected to this method. In the code above, you can see that we connect the myclickevent method to a button-click event.

 

4th Q: How do I access a XAML object in the background program code?

To access the XAML objects in the background program code, you need to define these objects using the same name as in The XAML file. For example, in the following code snippet, we name this XAML object as objtext. In the background program code, we also name this object with the same name.

Figure 2 access a XAML object in the background code

5th Q: What Is Silverlight?

Silverlight is a browser plug-in that enables you to implement a variety of animations, images, audios, and videos on webpages. In short, Silverlight is very similar to flash. We can watch the animation through flash and it also needs to install a plug-in the browser.

6th Q: Can Silverlight run on other platforms except Windows?

Of course, in addition to the Windows platform, animations made using Silverlight can also run on other platforms. Regardless of the platform, you only need to install the corresponding Silverlight plug-in.

7th Q: Since WPF can be run in a browser, why do we still need Silverlight?

Yes, there is an application called "WPF Browser Application" that can run WPF in the browser. For "WPF Browser Applications", you need to install. NET Framework on your target machine. However, for Silverlight, you only need a small plug-in. In other words, the "WPF Browser Application" is related to the operating system and depends on an operating system, while Silverlight does not. The Silverlight plug-in can run on other operating systems other than windows. However, as we all know,. NET Framework can only run on Windows operating systems. Therefore, when developing some network applications, we hope that the rich presentation technology brought by the Application of WPF and its operation can be cross-platform, so Silverlight is generated.

8th Q: What is the relationship between Silverlight, WPF, and XAML?
As mentioned above, XAML is an XML file that defines interface elements. This XML file can be read by the WPF framework or Silverlight framework and displayed on the interface. Microsoft first developed WPF and used XAML to describe interface elements for the WPF framework. Then, Microsoft expanded WPF and developed WPF/E, which can display the user interface in the browser. WPF/E is actually the development code name of Silverlight. Later, Microsoft officially released Silverlight.

Therefore, the XAML file only defines the XML structure used to represent the UI elements. Both WPF and Silverlight will read these UI elements and present them on their respective platforms.

Figure 3 Relationship Between Silverlight, WPF, and XAML

9th Q: What is the Silverlight architecture?

Before discussing the Silverlight architecture, let's take a look at the technologies that make up Silverlight. Silverlight can be said to be a hodgedge of technologies. It learns from and draws on many of Microsoft's existing mature technologies. We can regard the Silverlight plug-in as a collection of multiple technologies, including. NET Framework, vector animation, multimedia, and JavaScript.

Figure 4 Silverlight is a collection of technologies. net Framework core functions, Ajax, some functions from the Silverlight framework itself, such as animation, multimedia and so on. Specifically, the Silverlight architecture mainly includes the following four important aspects:

• Some. NET Framework components Silverlight uses some components from. NET Framework. One of the most important components is WPF. Many UI elements (such as check boxes, buttons, text boxes, etc.) and XAML file processing are all from the WPF system. In addition, Silverlight also uses other components. For example, Silverlight uses WCF for data access. The CLR is used for memory management, security check, and garbage collection. Network-related base classes are used for string processing,Algorithm, Performance, integration, and global support.

• The core of the presentation core displays 2D vector animation, images, multimedia, DRM, and the ability to process mouse and keyboard input.

• Other Technologies Silverlight also deal with other technologies, such as Ajax and JavaScript. Therefore, it also draws many functions from these technologies.

• Hosting Silverlight animation will eventually run in the browser environment, so it requires a hosting feature so that it can run applications in the browser and expose a Dom to JavaScript, javascript controls the Silverlight component through Dom. In addition, it has an installation function that can be used to install the Silverlight application and plug-in the browser environment. In the following Silverlight architecture diagram, we can see that the presentation core reads The XAML file for the interface display, while the XAML is. NET framework, but the final presentation of the interface is completed by the core of the presentation.

Figure 5 Silverlight Architecture

The Silverlight application is a typical HTML operating in a browser. The related labels will create the Silverlight program instance. When a user interacts with a Silverlight application, it sends events to the JavaScript or. NET System-depending on the development language you use. In JavaScript or. NET code, we can call the Silverlight function. The XAML file is read and processed by the Silverlight runtime, And the Silverlight application user interface is displayed based on the browser.

Figure 6 Silverlight Operating Mechanism 10th Q: How to create a simple Silverlight application?

The following example is created using Visual Studio 2008 web express and. Net 3.5. It takes six steps to complete our first Silverlight application. Let's take this example step by step:

Step 1: To develop a Silverlight application in Visual Studio, first install the Silverlight SDK. We can download it from Microsoft's website. Http://www.microsoft.com/downloads/details.aspx? Familyid = FB7900DB-4380-4B0F-BB95-0BAEC714EE17 & displaylang = en

Step 2: After you install the Silverlight SDK, you can use the Silverlight Project template. When creating a new project, you can see the "Silverlight application" Project template in the "Create a project" dialog box, as shown in:

Figure 7 create a new Silverlight Project

Step 3: When you click "OK" to close the "new project" dialog box, you will see a Silverlight application configuration dialog box, as shown in, which has three options:

Figure 8 Silverlight Project Properties

Add an ASP.. Net project to the solution to host Silverlight (add a ASP. net web project to the solution to host Silverlight):-This option is the default option, which creates a new Web application project, this project will host and run your Silverlight application. If you want to create a new Silverlight application, select this option.

Automatically create a test page during building to host Silverlight (automatically generate test page to host Silverlight at build time):-If you choose this option, visual Studio automatically creates a new test page every time you debug and test your application. If you want to focus on the development of your Silverlight application, you can select this option.

Connect this Silverlight control to an existing website (link this Silverlight control into an existing web site):-If you already have a Silverlight application, select this option, it will help you connect an existing Silverlight application to an existing web application project. This option is unavailable for new projects. You need an existing web application project.

In this example, we select the first option. When you click "OK" to close this dialog box, we will see the entire IDE environment developed by Silverlight, as shown in.

Figure 9 Silverlight Project Development Environment

Let's take a look at what Visual Studio has created for us. In the solution browser, you will see two projects: one is our web application project, and the other is the Silverlight application project. In a Silverlight application project, there are two XAML files: App. XAML and page. XAML. The app. XAML file contains global information.

Step 4: For convenience, we only use one textblock to display the text of a string. As you can see, when we input strings in the page. XAML file, these strings are also displayed in the XAML viewer. The code is synchronized with the view.

Figure 10 edit XAML

Step 5: Now, we need to embed the Silverlight application into An ASPX page. Therefore, on the HTML/ASPX page, we need to use the "register" attribute to reference the Silverlight namespace:

< % @ Register Assembly = "System. Web. Silverlight" Namespace = "System. Web. UI. silverlightcontrols" Tagprefix = "Asp" % >

At the same time, we also need to reference the script manager of the Silverlight namespace. The script Manager Control is a function of Ajax. Its main purpose is to manage download and reference JavaScript libraries.

< ASP: scriptmanager ID = "Scriptmanager1" Runat = "Server" > </ ASP: scriptmanager >

Finally, we need to reference the Silverlight application. In the following code, you can see that the xap file is referenced. The xap file is actually a Silverlight application compressed into ZIP format after compilation. In this package, it basically contains all the files required by the application. If you change the suffix to zip, you can use WinZip or WinRAR to open the file and view its content.

< ASP: Silverlight ID = "Xaml1" Runat = "Server" Source = "~ /Clientbin/myfirstsilverlightapplication. xap" Minimumversion = "2.0.31005.0" Width = "100%" Height = "100%" />

 

So far, the aspx/html code embedded in the Silverlight application looks like the following:

<% @ Page Language = " C # " Autoeventwireup = " True " %> <% @ Register assembly = " System. Web. Silverlight " Namespace = " System. Web. UI. silverlightcontrols " Tagprefix = " ASP " %> <! 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" Style = "Height: 100% ;" > < Head Runat = "Server" > < Title > Myfirstsilverlightapplication </ Title > </ Head > < Body Style = "Height: 100%; margin: 0 ;" > < Form ID = "Form1" Runat = "Server" Style = "Height: 100% ;" > < ASP: scriptmanager ID = "Scriptmanager1" Runat = "Server" > </ ASP: scriptmanager > < Div Style = "Height: 100% ;" > < ASP: Silverlight ID = "Xaml1" Runat = "Server" Source = "~ /Clientbin/myfirstsilverlightapplication. xap" Minimumversion = "2.0.31005.0" Width = "100%" Height = "100%" /> </ Div > </ Form > </ Body > </ Html >

Step 6: Finally, set the web application project as the startup project and set its page as the startup page. Run this project and you will see that your first Silverlight application has been successfully run.

Figure 11 Silverlight Running Effect

 

 

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.