Silverlight is a new Web rendering technology that can run on various platforms. With this technology, you will have an interactive experience with rich content and brilliant visual effects, whether in a browser, on multiple devices, or on a desktop operating system (such as Apple Macintosh). The rendering technology XAML (eXtensible Application Markup Language) in Microsoft. NET Framework 3.0 (Windows programming infrastructure) follows the Windows Presentation Foundation, which is the basis of the Silverlight rendering function.
This White Paper will guide you through the basics of Silverlight and how to use Microsoft's many tools, including Microsoft Expression blend, Microsoft Visual Studio 2005, and XAML, to build a rich graphic site. First, let's take a look at the background information about the development process of Silverlight and its position in the development field.
Evolution of web development: Turning to Web. Next
CERN's Tim Berners-Lee invented the modern Web as a system that allows the storage and link of static documents on a network-based system. In the next few years, with the development and maturity of innovation, "activity" Documents naturally become a new stage of modern web development, which will be generated upon receiving access requests, the document contains time-or user-specific information. CGI and other technologies have become the basis for implementation at this stage. With the passage of time, the function of generating documents on the Web has become extremely important, and the technical development has also gone through CGI, Java, ASP, and reached the ASP. NET stage.
ASP. NET has set a milestone in enabling developers to adopt the server development model and quickly develop high-quality web applications using the same best tools in the Visual Studio series.
It turns out that user experience is a major obstacle in Web applications. In this regard, technical restrictions make Web applications unable to provide the same rich user experience as client applications that use local data.
XMLHTTPRequest object (released by Microsoft as part of Internet Explorer 5 in 2000) has become the basis of Asynchronous JavaScript and XML (Ajax) technologies, this technology enables web applications to respond to user input more dynamically, because only a small part of the web page is refreshed and does not need to be reloaded. Innovative Ajax-based solutions (such as Windows Live local ing) allow web applications to go further and provide a user experience similar to that of clients.
Silverlight is the next development phase in which application developers and designers can present potential user experience to their customers. To achieve this, it allows designers to demonstrate their creativity and save their work in a format that can directly affect the web. In the past, designers used tools with rich output functions to design websites and user experiences. However, in terms of implementation capabilities of these designs, developers were limited by the web platform. In the Silverlight model, designers can build the desired user experience and express it as XAML. Subsequently, developers can use the Silverlight runtime to directly merge the XAML into a webpage. Therefore, the cooperation between the two can be closer than ever before, so as to provide a rich client user experience.
Because XAML is XML, It is text-based and provides firewall-compatible and easy-to-check instructions for these rich content. Although other technologies (such as Java applets, ActiveX and flash) can be used to deploy richer content than DHTML, CSS, and JavaScript, they send binary content to the browser. This makes it difficult to carry out security review, not to mention the difficulty of updating, because the entire application must be re-installed after any changes, which is not a friendly user experience, and the page may be stuck. If Silverlight is used, the server generates a new XAML file when you need to change the rich content. Next time you browse this page, the user will download this XAML and update the experience without any reinstallation.
The core of Silverlight is the browser enhancement module, which is used to render the XAML and draw the generated graphics on the browser interface. Its download volume is small (less than 2 MB). You can install it when you click a site containing Silverlight content. This module discloses the underlying framework of The XAML page to JavaScript developers to achieve page-level content interaction. In this way, developers can perform their own work, for example, you can write an event handler or use JavaScript code to process the XAML page content.
However, there have been enough theoretical discussions! Let's take a look at our first Silverlight project through practice.
Build a simple Silverlight Application
Let's take a look at Microsoft Expression blend and use this tool to create a very simple application in The XAML format for Silverlight. To create a Silverlight application in blend, select File> new project. The new project dialog box is displayed.
Select OK to create a new project. This project will contain a default HTML page, some JavaScript source code of the page, a XAML document, the JavaScript source code of the XAML document, and Silverlight. js.
Silverlight. js contains the code used to download and instantiate the Silverlight control. It is provided to users as part of the Silverlight SDK.
Default.html is a standard HTML webpage. The webpage contains three Javascript script references pointing to silverlight.js?default.html. JS (including application-specific code for installing Silverlight) and scene. XAML. JS (which contains the Event Handlers for the application events defined in XAML ).
It is designed as an independent page (default.html), separate from the instantiation logic (default.html. JS), design (scene. XAML), and Event code (scene. XAML. JS. However, there have been enough theoretical discussions. Now we are developing a simple application.
Create a UI for video players
Add a video file to the project. To do this, right-click the project files in the project files window in the upper-right corner of the screen, and select Add existing item... (add existing project ).
When you select a WMV file and add it to the project, the file is displayed in the project browser, and a media element is added to the view.
Now you can run your project. The browser will start and play your video!
You can edit XAML to stop automatic video playback. The XAML designer has two tabs on the right: Design and XAML. Select the "XAML" tab to open the XAML Editor, as shown in 3. Use this editor to edit the XAML text for the media element and add the property autoplay = false.
Now, if you run this application, you will see that the Silverlight content does not play although it presents the first frame of the video.
Add controls to a video player
Add two text blocks for the application. The text content is play and stop, and the names are txtplay and txtstop respectively. After the process is completed, the XAML should be as follows:
[Run code] [copy to clipboard] [±]
Code:
<Canvas
Xmlns = "http://schemas.microsoft.com/client/2007"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Width = "640" Height = "480"
Background = "white"
>
<Mediaelement autoplay = "false" X: Name = "movie_wmv" width = "320" Height = "240" canvas. left = "128" canvas. top = "56" Source = "movie. WMV "stretch =" fill "/>
<Textblock X: Name = "txtplay" width = "72" Height = "24" canvas. left = "136" canvas. top = "336" text = "play" textwrapping = "Wrap"/>
<Textblock X: Name = "txtstop" width = "80" Height = "24" canvas. left = "136" canvas. top = "368" text = "stop" textwrapping = "Wrap"/>
</Canvas>
Next, add the event handler declaration to the text block in XAML. To this end, you can use the mouseleftbuttondown attribute to declare the handler that clicks the mouse. In the txtplay text block, add an event handler for doplay; In the txtstop text block, add an event handler for dostop. After the process is completed, the XAML should be as follows:
[Run code] [copy to clipboard] [±]
Code:
<Textblock X: Name = "txtplay" width = "72" Height = "24" canvas. Left = "136"
Canvas. Top = "336" text = "play" textwrapping = "Wrap"
Mouseleftbuttondown = "javascript: doplay"/>
<Textblock X: Name = "txtstop" width = "80" Height = "24" canvas. Left = "136"
Canvas. Top = "368" text = "stop" textwrapping = "Wrap"
Mouseleftbuttondown = "javascript: dostop"/>
Now, if you click a text block, an event is triggered. You can use the JavaScript function to capture and process the event.
Processing events in Javascript
Scene. XAML. js created by the template can be used to capture and process user events in JavaScript. Since you have specified doplay and dostop event handlers in XAML, they should be implemented here. The Code is as follows:
[Run code] [copy to clipboard] [±]
Code:
Function doplay (sender, eventargs)
{
VaR thehost = Document. getelementbyid ("silverlightcontrol ");
VaR themedia = thehost. content. findname ("movie_wmv ");
Themedia. Play ();
}
Function dostop (sender, eventargs)
{
VaR thehost = Document. getelementbyid ("silverlightcontrol ");
VaR themedia = thehost. content. findname ("movie_wmv ");
Themedia. Stop ();
}
In this example, the Silverlight control is called silverlightcontrol, And the Javascript variable that references the control is called thehost. The preceding name will be used to search for media elements (movie_wmv in this example) later. When a movie is added to a project, this media element is created for you. The element name is based on the movie name. Therefore, if the movie name is movie. wmv, the media element is called movie_wmv. If other movies are used, the control also uses other names.
The media element can be played or stopped by using the play and stop methods.
Because the media element is referenced, you can call the preceding method to stop or start a movie, as shown in figure 4.
So far, you have built your first Silverlight application! For more resources about Silverlight, see the new Silverlight Developer Center and http://www.silverlight.net /.
Learn more about Silverlight calls
The HTML page calls createsilverlight () on the default.html. js source code page ().
[Run code] [copy to clipboard] [±]
Code:
SYS. Silverlight. createobjectex ({
Source: "scene. XAML ",
Parentelement: Document. getelementbyid ("silverlightcontrolhost "),
ID: "silverlightcontrol ",
Properties :{
Width: "100% ",
Height: "100% ",
Version: "0.9"
},
Events :{
Onload: SYS. Silverlight. createdelegate (scene, scene. handleload)
} // 51 aspx
});
This call uses many attributes, including those used to define the XAML, Silverlight control appearance, and onload and onerror event handlers to be rendered.
Source: property is used to define the XAML to be rendered by the Silverlight control. This attribute can be an external file (as shown in this example) or a name <SCRIPT> tag containing the XAML page.
When adding a Silverlight control on the page, add the control to the name <div>. Parentelement: attribute should be used as the <div> name.
The ID of the control is specified by the ID: property. 51 aspx
The physical properties (such as height, width, and version) of the control are set by the array in the property loaded to properties. To view all the attributes, see the Silverlight SDK documentation.
Conclusion
This White Paper provides a high-level overview of Microsoft Silverlight and describes how Silverlight applies to the next-generation Web Application Development stack. As you can see, XAML is like a consortium that combines designer specifications, developer tools, and user-oriented delivery. You have a general understanding of expression blend, how to use it to define the UI for webpages, and how to program them using JavaScript.
The content you learned in this article only covers the functionality of Silverlight. This technology contains many features that you can now use to start building the next web. This is an interesting journey. Come and try it! Ͷ ð ÿ bbs. 51aspx. com8ó63?