百度空间 | 百度首页 
 
查看文章
 
Silverlight入门教程Silverlight XAML is a subset of the WPF XAML language.
2008年12月18日 星期四 上午 10:53

Silverlight入门教程Silverlight XAML is a subset of the WPF XAML language.

History Remark

To understand the reason why Microsoft decided to introduce one more internet technology, we should look at history. In the beginning, there was SGML and then, based on it, HTML. HTML was used and is still being used as a great markup language for presenting data. When computers became faster and users needed more interaction with simple pages, a JavaScript (also called ECMAScript) was introduced. Web pages grew bigger and more complex and needed frequent updating, so such technologies as PHP, ASP and ASP.NET were born.

However, while web applications got richer, they needed more effort to support that richness. AJAX is the last approach used to make applications again more beautiful and powerful. Now lots of companies and developers are faced with the troubles that this power brings. It's hard to maintain large amounts of weakly typed JavaScript code. Not only that, but in every browser type your web application looks different, if it even works there at all. Furthermore, debugging a web application, especially if it is AJAX enabled, can become a real headache (but thanks for ASP.NET, it's a great pill for this headache).

All of these obstacles prevent companies and developers from creating really big and powerful web applications targeted for any-browser users. So, what will help us with this mess? Yeah, you are right: Silverlight will be our saver.

What is the silverlight?

Silverlight is a Microsoft technology that allows you to make rich applications with nice interfaces that are executed in a browser. "And what?" you'll probably ask.

Well, first of all, it's truly cross-platform and cross-browser. This means that if you write your application using Silverlight, it will run in such browsers as IE, Firefox, Opera (coming soon) and Safari. Not only that, but wherever you run your application it will look the same.

The second benefit that we will achieve with Silverlight is that applications can be written using any .NET language.No more JavaScript! However, if you like coding in JavaScript, you also can use Silverlight in version 1.1. Application design is based on a new markup language called XAML (pronounced "zammel"). Silverlight XAML is a subset of the WPF (Windows Presentation Foundation) XAML language. This means that once you've learned Silverlight, you can easily begin with WPF.

Versions

Today there are two known Silverlight versions: 1.0 and 1.1 Alpha Refresh. Version 1.0 is a start release that has XAML support and JavaScript for client-side coding. Version 1.1 is everything from 1.0 , plus .NET Framework.

Development with Silverlight

The best way to learn something is to use it. First of all, you must download Silverlight itself.I recommend that you download and install Visual Studio 2008 Beta 2 and Microsoft Silverlight Tools Alpha for Visual Studio 2008 Beta 2. In this case, creating a Silverlight application will be as easy as creating a simple .NET application. However, if you somehow don't have the possibility of using VS 2008, it's possible to create a Silverlight application in VS 2005. Be aware that it is quite tricky, though, and against VS 2008 you won't have IntelliSense enabled for writing XAML files. I will describe the workaround of creating a Silverlight project in VS 2005 further on.

Using Visual Studio 2008 Beta 2

Start Visual Studio and open a New Project wizard. Choose Silverlight Application under Visual C#. Enter the application name and click the OK button. After you've created a Silverlight Application project, you'll see in Solution Explorer the class library project with some files already added.To check that everything works fine, change the content of the Page.xaml file by adding a TextBlock element inside a Canvas element.Compile and run your project. If everything is right, you will see your first Silverlight Application.

When you build a Silverlight application, the same things happen as would in a simple class library. In fact, it is a class library. However, unlike a simple class library, it has references to other versions of mscorlib and other libraries. Those libraries are also simple .NET libraries. That is why we can use Visual Studio 2005 for Silverlight development even with .NET IntelliSense support: there is no difference between compiling common .NET applications and Silverlight applications. So, after building a project, we have a .NET library with application logic and a XAML file with its UI description. Note that XAML with a code-behind .NET class is something similar to an ASP.NET page with its code-behind file.


The next thing I should point out is how a Silverlight application is loaded by the client. Look at the TestPage.html page: there you'll see references to the Silverlight.js and TestPage.html.js script files. The only element in that page is <div> with the CreateSilverlight() function in it. This is the place where the creation of Silverlight applications takes place. When the page loads, the browser calls the CreateSilverlight() function, which is defined in the Default.html.js file. Let's have a look at this function:

//contains calls to silverlight.js, example below loads Page.xaml
function createSilverlight()
{
    Silverlight.createObjectEx(
    {
        source: "Page.xaml",
        parentElement: document.getElementById("SilverlightControlHost"),
        id: "SilverlightControl",
        properties:
        {
            width: "100%",
            height: "100%",
            version: "1.1",
            enableHtmlAccess: "true"
        },
        events: {}
    });
    // Give the keyboard focus to the Silverlight control by default
    document.body.onload = function()
    {
        var silverlightControl =
            document.getElementById('SilverlightControl');
        if (silverlightControl)
        silverlightControl.focus();
    }
}
It creates a Silverlight application with specified properties of XAML file, width, height, etc. This is the place where Silerlight.js code is used. Information about parameters passed to the Silverlight.createObjectEx() function can be found at the MSDN article Using CreateSilverlight.js and Silverlight.js. All it does is activate the browser's Silverlight ActiveX plug-in and run it with specified parameters. In this moment, "woo-hoo" takes place. Your library is loaded as a simple .NET assembly and Silverlight .NET runtime executes it. Everywhere. Yeah, this is the cross-platform .NET Framework. Microsoft placed the whole .NET Framework (of course, it has some limitations) in only 4 megabytes. That's awesome.

The processes that take place in your Silverlight application (e.g. loading, event handling) are mostly the same as in Windows Forms applications. A basic Silverlight application consists of a XAML file and a code-behind file. This behavior is similar to the ASP.NET model, where we have an ASPX file with some HTML and ASP.NET elements, as well as a code-behind file with page logic. So, everything you define in XAML you can access from code-behind programmatically.

When it comes around to coding your Silverlight application in .NET, then it becomes very similar to Windows Forms or ASP.NET. You have some controls on your canvas and you can assign handlers for the different events of those controls.

You can see that it will be really easy to learn programming with Silverlight if you are familiar with some basic principles of Windows Forms and/or ASP.NET application development.


类别:windows presentation foundation(wpf) & xaml | 添加到搜藏 | 分享到i贴吧 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu