Search
一步一步学Silverlight 2系列(1):创建一个基本的Silverlight应用
概述
Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章带您快速进入Silverlight 2开发。
本文为系列第一篇创建一个基本的Silverlight 2应用,不能免俗,从最简单的Hello Word 开始。
建立项目
安装完Silverlight 2 Beta 1之后打开VS2008,打开新建项目对话框,可以看到Silverlight Application项目模板。
Silverlight应用不能够独立运行,之后弹出的对话框中可供我们选择创建一个ASP.NET Web Site或者Web Application Project用来托管Silverlight应用程序。
这里我们选择创建一个Web Application Project,创建完成后的项目结构如下所示:
理解.xap文件
在建立一个Silverlight应用程序后,我们什么都不做,直接编译一下整个解决方案,可以看到在资源管理器中多出了一个ClientBin的文件夹,并在下面添加了一个TerryLee.SilverlightDemo2.xap的文件。
该文件是一个标准的.NET程序集,在编译的时候所有的XAML标识和资源文件如图片等都会包含在里面,采用了标准的Zip压缩算法,以减少客户端下载的文件体积。拷贝一份该文件,并且修改后缀名.xap为.zip,并且解压缩,可以看到里面包含了一些dll文件和一个AppManifest.xaml:
再打开TerryLee.SilverlightDemo2TestPage.aspx文件,在页面的顶部引入了System.Web.Silverlight程序集,支持<asp:Silverlight/>控件:
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls" TagPrefix="asp" %>
<asp:Silverlight/>控件的声明如下,其中属性Source属性指定了刚才编译生成的.xap文件的路径:
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/TerryLee.SilverlightDemo2.xap" Version="2.0" Width="100%" Height="100%" />
创建一个Hello Word程序
现在我们创建一个简单的Hello Word Silverlight程序,使用如下XAML创建一个简单的按钮:
运行后效果如下:
为按钮添加Click事件,在XAML编辑器中输入事件名称Click之后,再按Tab键将会使用默认的命名方法生成事件处理方法:
打开Page.xaml.cs文件后,可以看到已经生成了对应的事件处理方法,现在就可以用熟悉的C#来编写处理程序了,如单击按钮时我们改变按钮的背景色和文字:
private void myButton_Click(object sender, RoutedEventArgs e) { this.myButton.Content = "Clicked!"; this.myButton.Background = new SolidColorBrush(Colors.Red); }
结束语
本篇文章是使用Visual Studio 2008开发Silverlight 2应用程序的一个入门,相信大家都已经看过ScottGu的文章已经有所了解。但是为了整个系列完整起见,还是做了一下重复的劳动。


评论
The hottest new traditional and seasonal
coach outlet store online
. Keep up to date with the latest trends. Look at this zip top closure with colorful C logo printed fabric bag.You may find the most affordable Bags here.This brown handbag called Flagship Signature Brown Handbag has been the best seller in the
coach factory outlet
for almost three weeks.Everyone believes the fact that
coach outlet store online
can be well-known simply because of its high quality and the lovely pattern.My friend is dying to get a coach purse at
coach outlet store
because all of her friends have them at work. They earn great reputation from many people.The latest fashion collection contains those bags suitable to be worn in both casual and formal environment. We provide the best quality
louis vuitton uk
with the most reasonable price we can offer as you see in our online store.in fact,
louis vuitton
is one of the most famous fashion design master.he opened the fist suitcase shop called after his name.louis vuitton outlet
,welcome to buy urban louis vuitton on our online shop.discount price is our special offer, durability and high quality is our promise.They're not chosen, so one of these ideal for you.For more flexibleness a lot more like these, there are lots of discount 'shoulder' variations outlet
louis vuitton bags
.Show someone how much you care with this sophisticated and sparkling
christian louboutin
collection. Cut with exquisite detail, this can be a beautiful yet practical gift.All of the
armani watches
Denim handbags at the High quality, practical, timeless yet resolutely modern design with our professional and excellent service.You can choose the discount Louis Vuitton bags on our website. Cheap
emporio armani watches
are specifically designed to make a statement of fashion and elegance..With the safe door to door shipping, the
gucci outlet
will send the products to your hands, which are of top quality and at competitive factory prices.One of my friend wants to buy a gift for her mother on the Mother's Day. She asks me, I suggest her to buy
hermes bags
, which has good quality and unique design.I can't wait sharing the
hermes birkin
with you.It's an online crystal shopping paradise supplying delicate Swarovski jewelry.